Recommand · January 13, 2022 0

Stream providers and await in Flutter – wait till data is loaded

I am using StreamProviders in my Project and currently facing a problem, that the current setup doesn’t ket me wait till stiff is loaded before i show certain screens.

So here in my Home() i define my providers:

        providers: [
            create: (context) => DatabaseService(uid: user?.uid).currentUser,
            initialData: null,
            catchError: (_, __) {},
            create: (context) => DatabaseService(uid: user?.uid).petData,
            initialData: null,
            catchError: (_, __) {},
        child: Scaffold( ... ));

in my database.dart i’ve got:

    return ownersCollection.doc(uid).snapshots().map(_currentUserFromSnapshot);

  Stream<Pet> get petData {
    var map = petsCollection.doc(uid).snapshots().map(_petDataFromSnapshot);
    return map;

  PetUser _currentUserFromSnapshot(DocumentSnapshot snapshot) {
    var data = as Map;
    return PetUser(uid!, data['name']);

  Pet _petDataFromSnapshot(DocumentSnapshot snapshot) {
    var data = as Map;
    return Pet(uid!, data['petName'], null, null);

So what i want i the further screens to how Pet and PetUser data, nut only after I know the loading from DB is complete.

How do I change such setup for it to be possible.

For now in my Profile Screen i call:

final user = Provider.of<PetUser?>(context);

and if i print its value it is first null, and after a while filled with data.

I am super new to the topic, so I also would accept tipps on how to completely change my approach.