Flutter Mobx - Passing store state between pages in PageView - flutter

How can i pass or preserve the state of my store between pages using PageView?
By example, I'm trying to upload an image at the first page to edit, and pass to a second page where i want to show the image with a form. Im using MVC, and the image is set via controller (store), but it's not working. I've been made some tests and sometimes, at debugging runtime, the image is shown at the second page. But, sometimes even debugging it isn't shown and its value is null.
I tried with AutomaticKeepAliveClientMixin also, but the behavior is the same.
Someone can tell me what i'm doing wrong or what i'm missing?

I think using singleton can solve your problem, actually I think I had this problem in two apps I made.
I used get_it plugin, this package creates singletons easily for you and it's simple to retrieve anywhere in the app without using context.
First, register your MobX as Singleton, I do it on main:
getIt.I.registerSingleton<YourStore>(YourStore());
Retrieve the same instance of your Store, if you have observables in the first page and call an action on the second page, it will trigger the observables in the first page or wherever you used any observable from this instance of Store.
GetIt.I<YourStore>().changeImageFromFirstPage();
You can call GetIt.I().youraction() anywhere in your app, it will trigger the observables you created on this Store.

final list = Provider.of<TodoList>(context);
in the MobX document, there is a provider that can be used as the above code.
I use GetIt too, it is really awesome.
there is a sample for this in the following URL for the MobX:
https://mobx.netlify.app/examples/todos

Related

Is there a way to make Riverpod's StreamProvider go into loading state only on the initial call?

This is something I've wondered about for a while. It is hindering the UX to show a loading Icon whenever the stream changes when using {StreamProvider's name}.when method.
I can already make it work by using a StreamBuilder instead and using the StreamProvider's .stream property as the stream. This way, it only shows a loading icon when no data is present, and if new data arrives on top of old data, it shows the old data until the new data is "loaded".
But I find that as a workaround and was wondering if there is a cleaner way.
The version 2.0.0 updates how AsyncValue is dealt with, making what you're asking the new default behaviour.
You can try it out now using version flutter_riverpod: ^2.0.0-dev.0

Flutter Dartdoc: How to Document State Objects in Statefull Widgets?

This is a part of a project where i am building a clone of the Flickr App for educational purposes.
I am playing around for the first time with Dartdoc and Documentation concept in general, I managed to use Dartdoc and create a Doc folder with documentation for my project. However, for all my Stateful widgets, The widget itself has documented Properly however the state object (the object where I have my build methods, my initstate methods, and my state variables), I don't get any documentation for that, is that the default behavior or am I doing something wrong? I want to generate Documentation for this state object, the methods inside it, and the variables inside.
Apart From that, I am getting a couple of warnings, I will search further into those, and if can't manage to resolve them I will open a separate question.
Here is how I used Dartdoc:
Dartdoc command
End of Dartdoc command
Generated Doc
Generated Doc _ Methods
Forgive me if my question is stupid, it is my first time ever working with documentation :D
EDIT: A naive solution that I am doing, is that I moved most of my variables and methods to the widget object instead of my state object. Is this the right way to go about this?
Your State class is private (_FlickrCameraScreen), so it will not generate public documentation. If you want public documentation for it, you will need to rename it to FlickrCameraScreenState. You can give it a private constructor if you need to restrict creation to your FlickrCameraScreen widget.

How to delete data from StreamController in Flutter Provider?

I am using provider to build my application, so the data is added to StreamController, and each time i refresh my app, it will call an API, and then push data to StreamController, and the question is how to remove the data before replacing with the new one?
controller.add(user);
To iterate the answer from the comment. There is no need to remove any data added to the StreamController instead, just adding a new data again will remove the old data from there. You can do something like this:
controller.add(newOne);
You can also check this reference to understand the concept of Stream with examples.

How to reload the app after flutter localization load

I implemented app localization for a flutter app. Everything is working fine. But there's one issue. In the load method of my AppLocalization class, I call methods that return Futures which initiate my translations.
Now while the load method is initiating my translations, the build method of my main page is called which completes the build with old localizables. If I go to another page and come back, the page get translated since the localizables would already have been initialized.
To prevent this, I need to either pause the build method of the page which I'm not sure how to. Or retrigger the build method at the end of the load function in my AppLocalization class. I cannot navigate to that page in the AppLocalization class since I don't have access to build context there.
Anyone has had a similar challenge in their projects?

How to show previous url after user has canceled dialog with message from Activity#mayStop()?

In our app we need to check if the data is saved when we are in a particular place before navigating away from it. So the user should be able to negate a browser back button request. But by the time that the history value change event is received the url has already been changed. The History class doesn't seem to have a way to restore the url back. Anybody have any ideas?
In GWT 2.1 you get Activities and Places. And activity has a maystop method, which is exactly what you want, if I understand you correctly.
Use a window.onunload or window.onbeforeunload javascript callback to confrim/save state.
onbeforeunload example
I haven't actually implemented this behavior yet, but here is my plan and maybe it will work for you.
1) Each time you receive an onHistoryChanged event and decide to allow it, save the current historyToken in an instance variable somewhere.
2) Keep track of activity on the page that should block navigation. Use a data structure that can keep track of multiple activities, like multiple file uploads, multiple edits, etc.
3) When you receive a new onHistoryChanged event, if your data structure from #2 indicates that it's not safe to navigate, avoid changing the page and restore the historyToken that you saved in #1. I'm assuming that you can do this either by:
a) Calling History.newItem(oldHistoryToken, false) or
b) Calling History.newItem(oldHistoryToken, true) and keeping a flag to force the next onHistoryChanged to be ignored.
Again, I haven't actually implemented this so let me know how it works out.
If you have links that allow the user to leave the app and you want to prevent that as well, you'll need to also add an onbeforeunload.
Have a look at the PlaceManagerImpl class from the gwt-platform framework. Especially the onValueChange() method and the methods dealing with the onLeaveQuestion field.
Hope that helps.
In this issue report, t.broyer explains in his comment that such behavior was planned during design of Places framework. The most important part is:
mayStop was a mistake, or it should have only been called when unloading the app, not for internal navigation within the app.
So probably it's better to not use it at all...