I use Firebase Authentication and Cloud FireStore plugin. I want to use the best practice and it's very hard to find a good example of what i try to do. Every example on internet show using Auth only or using Firestore only.
But what if want to have a profile page of the currently logged user? I first need to retrieve the current userID so do i use a FutureBuilder ? and now i want to result from that user so do i use a StreamBuilder right after? It's really hard to understand best practice about flutter.
Some people say to do it in a statefullwidget, put a reference of the snapshot in the initstate Other people are using Statefull Widget.
Here is a code example of what i try to achieve but i do think it's the dumb way.
PS: AuthHelper().uid()
return the UID as a string. I created a function for this i don't know how to do it in 1 line with FireAuth.
class EducatorScreen extends StatelessWidget {
static const routeName = '/educatorScreen';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: geAppBar(context, 'Contrôle Éducateur'),
body: FutureBuilder(
future: AuthHelper().uid(),
builder: (BuildContext context, AsyncSnapshot<String> uidSnapshot) {
if (uidSnapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
}
return StreamBuilder(
stream: Firestore.instance
.collection('users')
.document(uidSnapshot.toString())
.snapshots(),
builder: (ctx, educatorSnapshot) {
if (educatorSnapshot.data['favoriteNursery'] != 'none') {
return nurseryWidget(
context, educatorSnapshot.data['favoriteNursery']);
} else {
return noNurseryWidget(context);
}
});
}),
);
}
}
class AuthHelper {
Future<String> uid() async {
// Return UserID
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
return user.uid;
}
}