My Flutter application needs to listen for any changes in a specific Firestore document.
The process is quite simple and is found here in various solutions:
- StackOverflow: flutter-firestore-how-to-listen-for-changes-to-one-document
- StackOverflow:can-i-listen-to-a-single-document-in-firestore-with-a-streambuilder
- StackOverflow:using-stream-building-with-a-specific-firestore-document
- StackOverflow: how-to-get-a-specific-firestore-document-with-a-streambuilder
- Medium: how-to-get-data-from-firestore-and-show-it-on-flutterbuilder-or-streambuilder-e05
These are all solutions to using a Stream
& StreamBuilder
and listening for any changes.
Commonly one uses this approach:
Stream<UserModel?> getFirestoreUser(String uid) {
return FirebaseFirestore.instance.collection('users').doc(uid).snapshots().map((event) => event.data()).map((event) => event == null ? null : UserModel.fromJson(event));
}
where:
UserModel
has afromJson(Map<String, dynamic>)
factory constructor- and where there exists a
users
collection with the document ID beinguid
.
Stream returns a Stream<UserModel?>
that can be used later by a Provider.of<UserModel?>(context)
or in a StreamBuider<UserModel?>
Problem:
TL;DR - after a Firestore().getCurrentUser(uid)
stream is updated (directly, or manually using firestore emulator and changing the document values), the StreamBuilder<UserModel?>
below doesn't update.
At all points in app lifecycle (authenticated or not), the stream always returns null (however manually invoking the stream with .first
returns the user content, but .last
just hangs). See below for more info.
Link to github project.
To get started:
- Throw in your own
gooogle-services.json
file intoandroid/app
folder - change all references for
com.flutterprojects.myapp
to${your google-services.json package name}
- Use local emulator to update firestore changes
More Details:
My issue uses a similar approach, yet the Stream
doesn't return anything. My Flutter widget LoggedIn()
is used when the FirebaseAuth.instance.currentUser
is valid (i.e. not null)
class LoggedIn extends StatelessWidget{
@override
Widget build(BuildContext context) {
// TODO: implement build
return StreamBuilder<UserModel?>(
builder: (context, snapshot) {
print("Checking UserModel from stream provider");
if (snapshot.data == null) {
print("UserModel is null");
return UiLogin();
}
print("UserModel is NOT null");
return UiHome();
},
);
}
}
After a successful authentication, the text Checking UserModel from stream provider
is printed to the console, then UserModel is null
. At no point does UserModel is NOT null
get shown.
After successful authentication (i.e. user registered & document updated), I have a button that I manually invoke the stream and get the first item:
var currentUser = Firestore().getCurrentUser(FirebaseAuth.instance.currentUser!.uid);
var first = await currentUser.first;
print(first);
which returns the UserModel
based on the newly added user information.
When running the following code, execution stops on .last
, no error, no crash, no app exit, just stops executing.
var currentUser = Firestore().getCurrentUser(FirebaseAuth.instance.currentUser!.uid);
var last = await currentUser.last;
print(last);
BUT
When adding the following, any changes gets updated and printed to the console
Firestore().getCurrentUser(currentUid).listen((data) {
print("Firestore Data Changed");
print("Firestore change value: $data");
});
This means the StreamBuilder<UserModel?>
isn't working correctly, as the Stream<UserModel>
events do listen()
to changes and acknowledge those changes, but the StreamBuilder<UserModel?>
changes do not get affected and consistently returns null
.
Am I doing something wrong?