Why can't i use the method data() in my streambuilder in flutter

116

Well to be clear if you don't specify the StreamBuilder type it will be an <AsyncSnapshot> by default The solution was to put the StreamBuilder as a <DocumentSnapshot>

 StreamBuilder<DocumentSnapshot>(
            stream: usersDb.doc(_auth.currentUser!.uid).snapshots(),
            builder: (context, currentUserDocSnapshot) {
              if (!currentUserDocSnapshot.hasData) {
                return Center(
                    child: CircularProgressIndicator(
                  backgroundColor: Colors.deepPurple,
                ));
              }
              return Text(userSelectedSnapshot.data!["username"]);
            },
          );

You can then access the .data method and access your document values

Share:
116
Arnaud NaNo
Author by

Arnaud NaNo

Updated on January 04, 2023

Comments

  • Arnaud NaNo
    Arnaud NaNo over 1 year

    Why can't i use the method .data() ?

    StreamBuilder(
              stream:
                  usersDb.doc(widget.allUsersFromDb.docs[index]["uid"]).snapshots(),
              builder: (context, snapshot) {
                if (!snapshot.hasData) {
                  return Center(
                      child: CircularProgressIndicator(
                    backgroundColor: Colors.deepPurple,
                  ));
                }
                // List? invitedByArray = snapshot.data!.data() not working
                return Text("Invite");
              },
            ),
    
    • Wilson Toribio
      Wilson Toribio about 2 years
      What error is it showing when you use data()?
    • Arnaud NaNo
      Arnaud NaNo about 2 years
      The method data() isn't defined for the type Object
    • Arnaud NaNo
      Arnaud NaNo about 2 years
      found my issue the snapshot was an async snapshot by default i need to declare it as a documentSnapshot
  • Community
    Community about 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.