Punreach Rany
Punreach Rany

Reputation: 3700

Flutter & Firebase : How do I get a specific field from a specific document into a stream?

I have been trying to get a specific field from a specific document into a stream. But I always got null. How do I fix this?

Function

Stream<List<String>> getError() {
    DocumentReference errorReference =
        appSettingCollection.document('report_error');

    final Stream<DocumentSnapshot> snapshots = errorReference.snapshots();

    return snapshots.map((doc) {
      //print(doc.data['types']);
      return doc.data['types'];
    });
  }

Main

return StreamBuilder<List<String>>(
        stream: User_DatabaseService().getError(),
        builder: (context, snapshot) {
          
          final List<String> errorTypes = snapshot.data;
          print('In Error Report : $errorTypes'); // I got null??? But why?
///--------

}}

Upvotes: 0

Views: 941

Answers (1)

OmkarKhilari
OmkarKhilari

Reputation: 62

I think you have taken wrong stream. Try to correct Stream

StreamBuilder<DocumentSnapshot>(
      stream: Firestore.instance
          .collection('CollectionName')
          .document("DocumentID")
          .get(),

And use it below for accessing specific field as:

document["FieldName"]

For accessing single field you will need to access whole document.I will suggest to use FutureBuilder follow the link below and you will get your answer more precise.

https://firebase.flutter.dev/docs/firestore/usage/

Upvotes: 1

Related Questions