Arnaud NaNo
Arnaud NaNo

Reputation: 55

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

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");
          },
        ),

Upvotes: 1

Views: 350

Answers (1)

Arnaud NaNo
Arnaud NaNo

Reputation: 55

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

Upvotes: 2

Related Questions