1

I have some problems with using firebaseUser ID in my app in order to get information from firebase. I created tabBar which switches between StreamBuilders which store a list of blocks based on data from Firestore according to user Id taken from authStateChanges.

My functions which gets data:

 getFavSalons(AsyncSnapshot<QuerySnapshot> snapshot) {
return snapshot.data.docs
    .map((doc) => SalonBlock(
          salonName: doc.data()["salonName"],
          location: doc.data()["location"],
          workTime: doc.data()["workTime"],
          rating: doc.data()["rating"],
        ))
    .toList();  }   

  getFavMasters(AsyncSnapshot<QuerySnapshot> snapshot) {
return snapshot.data.docs
    .map((doc) => MasterBlock(
          fullname: doc.data()["fullName"],
          bio: doc.data()["bio"],
          rating: doc.data()["rating"],
        ))
    .toList();   }

My StreamBuilder in TabBarView:

Container(
            height: screenHeight * 0.9,
            width: screenWidth,
            child:
                TabBarView(controller: _controllerTabs, children: <Widget>[
              StreamBuilder(
                  stream: FirebaseAuth.instance.authStateChanges(),
                  builder: (context, AsyncSnapshot<User> snapshot) {
                    if (snapshot.hasData) {
                      return StreamBuilder(
                          stream: FirebaseFirestore.instance
                              .collection("customers")
                              .doc(snapshot.data.uid)
                              .collection("favSalons")
                              .snapshots(),
                          builder: (context,
                              AsyncSnapshot<QuerySnapshot> snapshot) {
                            if (snapshot.hasData) {
                              return Container(
                                margin: EdgeInsets.only(
                                    bottom: screenHeight * 0.33),
                                child: new ListView(
                                  children: getFavSalons(snapshot),
                                ),
                              );
                            }
                            return LoadingSalon();
                          });
                    }
                   return Text("Loading user...");
                  }),
              StreamBuilder(
                  stream: FirebaseAuth.instance.authStateChanges(),
                  builder: (context, AsyncSnapshot<User> snapshot) {
                    if (snapshot.hasData) {
                      return StreamBuilder(
                          stream: FirebaseFirestore.instance
                              .collection("customers")
                              .doc(snapshot.data.uid)
                              .collection("favMasters")
                              .snapshots(),
                          builder: (context,
                              AsyncSnapshot<QuerySnapshot> snapshot) {
                            if (snapshot.hasData) {
                              return Container(
                                margin: EdgeInsets.only(
                                    bottom: screenHeight * 0.33),
                                child: new ListView(
                                  children: getFavMasters(snapshot),
                                ),
                              );
                            }
                            return LoadingSalon();
                          });
                    }
                    return Text("Loading user...");
            ]),

When I run my app at the beginnig data exists in blocks, but when I try to switch between tabs there is an error "StreamBuilder returned null". I think that there is some troubles with authStateChanges(). How can I fix this issue?

Alex
  • 37
  • 2
  • 6

1 Answers1

1

This can happen because TabBarView recreates your tabs when you switch them. There are different solutions to this problem, not to duplicate answers check them out here: Preserving state between tab view pages

galloper
  • 813
  • 1
  • 9
  • 17