I've found myself at a dead end with some functionality I am trying to write and I am hoping for some assistance. The code itself is rather cumbersome so I'm going to do my best to summarize the idea of what I am trying to accomplish and provide a rough example.
Please let me know if there are things I can do to make it easier to help.
Problem:
I have 4 states: Initial, InProgress, Success, and Failure.
One of my widgets (flutter_slidable) has a parameter that requires me to return a true or false value and if it receives a null value it acts like it received false.
All of this code is nested inside of a BlocConsumer.
If I simply do an if statements waiting for Success or Failure it will instead return null because the state first moves to InProgress, and then when it finally switches to Success/Failure the widget no longer cares and has moved on with the null value as a result of the InProgress state.
Question:
Is there a way that I can await for a _specific_ state rather than taking an action based on each state?
Preemptive thanks to anyone who can help!
What I have:
Widget(
onWillDismiss: (actionType) {
if (state is Success) {
return true;
}
if (state is Failure) {
return false;
}
state is actually InProgress so it ends up as null
}
)
An idea of what I want:
Widget(
onWillDismiss: (actionType) {
[wait for or loop until state is Success/Failure] {
if (state is Success) {
return true;
}
if (state is Failure) {
return false;
}
}
}
)
Hi @TheMeanCanEHdian 馃憢
Thanks for opening an issue!
Are you able to make a sample project which illustrates the issue and share the link? Thanks 馃憤
Yes, I had a busy day so I didn't get to this but I will get you a small "working" example. Thanks!
Hey @felangel
So by breaking this down into a smaller working example I appear to have run into a more root problem with my Bloc state management before I even got to the function I described above. So first a thank you for making me break this down into basics so I can start with the right questions.
Do let me know if I should spin this into another issue as it is unclear at this time if the title will still be relevant.
Here is a link to a repository containing the lib folder and pubspec.yaml for running this code.
Summary:
I have 2 Blocs in this example code; one (ListBloc) that would be for fetching a list of items from a server and one (RemoveBloc) that would be for removing individual items from the server. The UI acts as a way to see these items and initiate a removal.
I have elements of the UI that I want to react to the current ListBloc as well as the RemovalBloc. Mainly a few CircularProgressIndicators and also the ability to cancel the dismissal if the Removal fails (This is what my initial question revolved around but I commented the code out at this time for reasons below).
Issue:
It appears that the earliest issue I run into when I am nesting bloc builders in order to try and have access to each of their states to take the above-mentioned actions.
I'm confident that at least one of my issues is with how I am using these BlocBuilders. When either the line 56 of slidable_card.dart is commented out or I remove the nested RemoveBloc BlocBuilder all together the dismissal animates as expected. If both those items are present then it fails to do so.
Please let me know if I can offer more information. I have attempted to comment my code as effectively as possible to help.
It looks to me like you are using the Bloc synchronously.
You expect that the bloc state has been updated before onWillDismiss is called, that is unlikely, in my opinion.
I would probably use a BlocListener and then dismiss or not according to RemoveSuccess and RemoveFailure there.
Does that make sense and achieve what you need?
Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 馃憤
Thanks @Jomik for taking the time to answer, I really appreciate it 馃檹
Thank you for your reply, the concept of using the BlocListener makes sense to me. I eventually found out that of one the primary issues I was having was a result of the widget being nested in a BlocBuilder for RemoveBloc causing it to rebuild when I would try and remove.
Hopefully, this all carries over to my actual application.
Thank you.
Most helpful comment
Thank you for your reply, the concept of using the
BlocListenermakes sense to me. I eventually found out that of one the primary issues I was having was a result of the widget being nested in aBlocBuilderforRemoveBloccausing it to rebuild when I would try and remove.Hopefully, this all carries over to my actual application.
Thank you.