StreamBuilder คืนค่า null

Aug 31 2020

ฉันมีปัญหากับการใช้ firebaseUser ID ในแอปของฉันเพื่อรับข้อมูลจาก firebase ฉันสร้าง tabBar ซึ่งสลับระหว่าง StreamBuilders ซึ่งเก็บรายการบล็อกตามข้อมูลจาก Firestore ตามรหัสผู้ใช้ที่นำมาจาก authStateChanges

ฟังก์ชั่นของฉันที่รับข้อมูล:

 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();   }

StreamBuilder ของฉันใน 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...");
            ]),

เมื่อฉันเรียกใช้แอปของฉันที่ข้อมูล beginnig มีอยู่ในบล็อก แต่เมื่อฉันพยายามสลับระหว่างแท็บเกิดข้อผิดพลาด "StreamBuilder ส่งคืนค่า null" ฉันคิดว่ามีปัญหากับ authStateChanges () ฉันจะแก้ไขปัญหานี้ได้อย่างไร

คำตอบ

1 galloper Aug 31 2020 at 18:35

สิ่งนี้สามารถเกิดขึ้นได้เนื่องจาก TabBarView สร้างแท็บของคุณขึ้นใหม่เมื่อคุณเปลี่ยนแท็บ มีวิธีแก้ไขที่แตกต่างกันสำหรับปัญหานี้ไม่ให้คำตอบซ้ำลองดูที่นี่: การรักษาสถานะระหว่างหน้ามุมมองแท็บ