marcglasberg / async_redux

Flutter Package: A Redux version tailored for Flutter, which is easy to learn, to use, to test, and has no boilerplate. Allows for both sync and async reducers.
Other
230 stars 41 forks source link

After push a new page, and Dispatching an action, there is an infinite loop #150

Closed biaarno8 closed 7 months ago

biaarno8 commented 7 months ago

I try to explain my problem. After the user logs in, the dashboard page is pushed. the bug is this: in the dashboard you should see the user's data but instead it remains in a loop loading state. With debugging I saw that it enters in the reduce method (where is the API calls), but then it freezes and doesn't continue and the calls don't start, and there isn't any error. Can you help me?

marcglasberg commented 7 months ago

Sure, I can. But I need to see the code. Can you please post here:

marcglasberg commented 7 months ago

@biaarno8

biaarno8 commented 7 months ago

Hi @marcglasberg I'm sorry if I answer now, but I'm writing from Italy, so there's a time difference. I can't post my code because it is protected by secrecy, but I can explain all steps to you. When the app is launched, on the first screen you can choose the language, after choosing the language, a loading page is pushed (it is used to have time to load the correct labels and also to retrieve other information from API calls), and after this loading page there is the login. From the login page, there is a button where you can return to the language selection screen to change the language. The bug is this: if from the login screen I go to the language selection screen again and choose the language, the loading screen is pushed, but it remains in an infinite loading state, but if I kill the app and I reopen the app, then it works. Now I'll explain the logical part. When the language is chosen, an action is called where in the reduce method calls API are started to get the labels and the theme of the app. I inserted a ChangeNotifier which is called when the labels and the theme are saved locally to update instantly the app. The changeNotifier listener was placed in the main.dart. In the main.dart the materialApp is wrapped inside the StoreProvider. So every time that data is saved locally, the main.dart is notified and rebuilt. After choosing and saving, the loading page is pushed, where an action is called to obtain information, but the API calls doesn't start. To make me understand better: the method before is called in the action (which has (dispatch(WaitAction.add(ID)) inside it), but the after method is never called (which has dispatch(WaitAction.remove(ID) inside ))). It's not a problem of API calls, because they don't start at all. Another thing: to navigate between pages I use the Fluro library. In my opinion, I believe that the problem is linked to the fact that the StoreProvider in the main.dart is recreated several times each time, and this creates problems. From what I've seen in the asyncRedux examples and documentation, I think it's not recommended to recreate the StoreProvider multiple times.

marcglasberg commented 7 months ago

If I can't see the code it gets very difficult to help you.

Try not rebuilding the store provider. Put it more to the top of the widget tree, so that your ChangeNotifier doesn't rebuild it. Try putting the ChangeNotifier below the StoreProvider. I'd suggest you check this example app. It shows the recommended way to implement Async Redux, so you can compare it: https://github.com/marcglasberg/SameAppDifferentTech/blob/main/MobileAppFlutterRedux/README.md

See:

Anyway, if before is being called but after is not, this can also mean that the action is never ending. The after method gets called even it the reducer throws an exception. So you should check that any Futures you await in the reducer are actually ending. Maybe add a timeout there.

Also, for internationalization and themes, check out these packages:

I am going to close this issue, but feel free to ask more questions.

biaarno8 commented 7 months ago

@marcglasberg but if I add a timeout it's not a solution, because in this way I can't load a data from API service. I try to putting the ChangeNotifier below the StoreProvider, but it still doesn't work. Maybe I was wrong, and the problem is in another point... it's very crazy and weird this bug!

marcglasberg commented 7 months ago

Sure, I told you to put a timeout not as a solution, but just to find out if that was the problem.

Anyway, if you decide to show me the code here, let me know. Maybe ask permission to your manager? You can simply delete the comment with the code after I see it.

biaarno8 commented 7 months ago

Hi @marcglasberg I wanted to tell you that I understood what the real problem was: the navigation library. In any case, your help was fundamental because I was too focused on StoreProvider, but instead I understood what the real bug was. Thank you!! :)

marcglasberg commented 7 months ago

Ok, glad you solved the problem!