I'm creating a flutter app using the Firebase authentication and firestore database to store the user profile.
When the user is authenticated I want to grab the user's profile document as a stream and make it available to all the widgets.
This is my code:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
StreamProvider<FirebaseUser>.value(
value: FirebaseAuth.instance.onAuthStateChanged),
StreamProvider<Profile>.value(value: DatabaseService().getProfile()),
],
child: MaterialApp(
title: 'App Title',
theme: customTheme(),
home: Wrapper(),
));
}
}
And inside the DatabaseService I have.
Stream<Profile> getProfile() {
final CollectionReference profileCollection =Firestore.instance.collection('profiles');
Stream<FirebaseUser> user = FirebaseAuth.instance.onAuthStateChanged;
user.listen((event) {
profileCollection.document(event.uid).get().then((value) {
return Profile.fromJson(value.data);
});
});
return null;
}
The problem is the getProfile method always returns null.