Home > Enterprise >  how to retrieve single field data of type number from firebase in text widget flutter
how to retrieve single field data of type number from firebase in text widget flutter

Time:03-11

I just want to retrieve a field from firebase firestore document which is <String, dynamic>, please tell me whats wrong with the code.

StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection('qazanamazcalc')
              .snapshots(),
          builder: (ctx, AsyncSnapshot snap) {
            return Text(
              snap.data.docs['countcalc'],
              // snapshot.data['countcalc'],
            );
          },
        ),

I am getting these two errors 'docs' method not found Receiver: null Arguments: [] and Expected a value of type 'int', but got one of type 'String'

CodePudding user response:

First of all, are you using a stream, which is a continuous data stream that listens to changes in a collection or a specific document. If I understand your question correctly you just want to do one fetch on a specific document.

FutureBuilder<DocumentSnapshot<Map<String, dynamic>>>(
  future: FirebaseFirestore.instance.collection('qazanamazcalc').doc('doc_id').get(),
  builder: (_, snapshot) {    
    if (snapshot.hasData) {
      var data = snapshot.data!.data();
      var value = data!['countcalc'];
      return Text(value);
    }

    return Center(child: CircularProgressIndicator());
  },
)
  • Related