Getx: How to listen for Firestore changes?

Created on 22 Jun 2020  路  4Comments  路  Source: jonataslaw/getx

Is there a way to use getX reactively for state management? Lets say my collection changes in Firestore and I want my UI to react automatically.
Also are you planning on adding persistence for state management just like hydrated_bloc exists for flutter_bloc? It would be great to continue from where user left off when restarting the app.

Most helpful comment

Wait the firestore has nothing to do with auth right? It's the same principle tho I guess. You need to have a stream which you get from your snapshot and have your obs value listen to that stream.

final int _someInt = 5.obs;
final Stream<int> _yourStream;

YourController(this._yourStream);

@override
void onInit()
{
  _someInt.bindStream(_yourStream);
  ever(_someInt , (value) => print(_someInt));
}

I typed this here not in a IDE so it probably will have mistakes, but you get the point

Note: The injected value called value will be null, I think this is a bug right now. So just reference the obs directly

All 4 comments

I think it depends on the Firebase. As long as they have any stream (ex. Firebase.onAuthStateChanged), then we can listen to it using .obs and ever combo.

Check this example https://github.com/jonataslaw/get/issues/215#issuecomment-641611736

Wait the firestore has nothing to do with auth right? It's the same principle tho I guess. You need to have a stream which you get from your snapshot and have your obs value listen to that stream.

final int _someInt = 5.obs;
final Stream<int> _yourStream;

YourController(this._yourStream);

@override
void onInit()
{
  _someInt.bindStream(_yourStream);
  ever(_someInt , (value) => print(_someInt));
}

I typed this here not in a IDE so it probably will have mistakes, but you get the point

Note: The injected value called value will be null, I think this is a bug right now. So just reference the obs directly

I think michael-ottink's answer perfectly answers your question.
Just use bind and join the firestore stream with a .obs variable.
Closing it.

checkout this method for the stream with firebase, link

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoldenSoju picture GoldenSoju  路  3Comments

DarkHeros09 picture DarkHeros09  路  3Comments

omartinma picture omartinma  路  3Comments

rupamking1 picture rupamking1  路  3Comments

wailashraf71 picture wailashraf71  路  4Comments