Tizi Dev
Tizi Dev

Reputation: 311

Define number of Firestore documents in Snapshot.data.lenght flutter

I my app user are going to create by TextForm Field a collection into FIrestore and this collection has some documents. Into my StreamBuilder I have set up stream and I can get the documents but I cant retrieve the Number of the documents created by the user logged with snapshot.data.lenght which I get the error:

Class 'DocumentSnapshot' has no instance getter 'lenght'.
Receiver: Instance of 'DocumentSnapshot'
Tried calling: lenght

The code:

class CollectData extends StatefulWidget {
  @override
  _CollectDataState createState() => _CollectDataState();
}

class _CollectDataState extends State<CollectData> {
  final String phone;
  final String wife;
  final String location;

  _CollectDataState({this.phone, this.wife, this.location,});

  Stream<DocumentSnapshot> getDatabase() async* {
    FirebaseUser user = await FirebaseAuth.instance.currentUser();
    yield* Firestore.instance
        .collection('dataCollection')
        .document(user.uid)
        .snapshots();
  }

  @override
  Widget build(BuildContext context,) {
    return StreamBuilder(
      stream: getDatabase(),
      builder: (context, snapshot,) {
        if (snapshot.data != null) {
          return Column(
            children: <Widget>[
              Container(
                height: 500,
                child: ListView.builder(
                  shrinkWrap: true,
                  itemCount: snapshot.data.lenght,
                  itemBuilder: (BuildContext context, int) {
                    return Card(
                      color: Color(0xFF1f2032),
                      elevation: 15,
                      child: Container(
                        width: 60,
                        height: 60,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: <Widget>[
                            Card(
                              color: Color(0xfffeaf0d),
                              child: Container(
                                  height: 40,
                                  width: 40,
                                  child: Icon(
                                    Icons.contacts,
                                    color: Colors.white,
                                    size: 25,
                                  )),
                            ),
                            Text(
                              snapshot.data['phone'],
                              style: TextStyle(
                                  color: Colors.white,
                                  fontWeight: FontWeight.bold),
                            ),
                          ],
                        ),
                      ),
                    );
                  },
                ),
              ),
            ],
          );
        } else
          return NoData();
      },
    );
  }
}

Upvotes: 3

Views: 4420

Answers (3)

John
John

Reputation: 343

A lot of things have changed in the newest version of Flutter and Firebase.

Where you call the StreamBuilder you have to pass in the QuerySnapshot type like this:

return StreamBuilder<QuerySnapshot>(...

And change your itemCount line to:

itemCount: streamSnapshot.data!.docs.length,

That should solve the problem for you.

Upvotes: 2

Peter Haddad
Peter Haddad

Reputation: 80914

DocumentSnapshot doesn't have any length property because, when you are doing this:

  Stream<DocumentSnapshot> getDatabase() async* {
    FirebaseUser user = await FirebaseAuth.instance.currentUser();
    yield* Firestore.instance
        .collection('dataCollection')
        .document(user.uid)
        .snapshots();
  }

It means you are only retrieving 1 document, since each document id is unique then the above will give you only one document.

You can add here:

itemCount: 1

If you want a list of documents then you have to do the following:

  Stream<QuerySnapshot> getDatabase() async* {
    FirebaseUser user = await FirebaseAuth.instance.currentUser();
    yield* Firestore.instance
        .collection('dataCollection')
        .snapshots();
  }

and then in itemCount:

itemCount: snapshot.data.documents.length

Upvotes: 1

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9735

there seems to be typo in lenght, you meant length, right?
your error message clearly complaining about typo

Class 'DocumentSnapshot' has no instance getter 'lenght'.

plz just try replacing this line itemCount: snapshot.data.lenght, with itemCount: snapshot.data.length,

Upvotes: 0

Related Questions