How to delete data from StreamController in Flutter Provider? - flutter

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.

Related

how to retrieve data from firebase to flutter?

I want to get data from firebase to my flutter app. I created a collection at firebase but don't know how to use it on flutter. Already I've done the setup process between flutter and firebase and also added necessary dependencies. but now I need help to get data from firebase. collection name '0xethocity' ;
Here's the documentation, try to implement it by yourself. Let us know if you get stuck.
https://firebase.google.com/docs/flutter/setup?platform=ios

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 Mobx - Passing store state between pages in PageView

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

Swift/Firebase add only new data [duplicate]

According to Firebase docs:
ChildAdded is triggered once for each existing child and then again every time a new child is added to the specified path
So, I have an app, that has a little banner at the top that pops up every time a user gets a new message. As you could have guessed, these messages are stored in a child in the user object in Firebase. So, here's the problem, when I load the app, it pops up for EVERY message the user has. Is it possible to have this observe event ONLY be called when a new child is added? I don't want it to be triggered for every single existing child, only when a new one is added. I would hate to have to store message references in core data, and do a check for every child to see if it already exists in core data :/
A few ways to do this:
Use ref.queryLimitedToLast(1) when you start. See Firebase, only get new children.
keep track of the most recent key you've got in your local storage and then query with queryOrderByKey().queryStartingAtValue(latestKey)
Add a timestamp to the items and then queryOrderByChild("timestamp").queryStartingAt(now) for items with timestamp >= now. See how to discard initial data in a Firebase DB

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...