简体   繁体   中英

Query a single document from Firestore in Flutter (cloud_firestore Plugin)

Edit: This Question is outdated, and I am sure, new documentation and more recent answers are available as of now.

I want to retrieve data of only a single document via its ID. My approach with example data of:

TESTID1 {
     'name': 'example', 
     'data': 'sample data',
}

was something like this:

Firestore.instance.document('TESTID1').get() => then(function(document) {
    print(document('name'));
}

but that does not seem to be correct syntax.

I was not able to find any detailed documentation on querying firestore within flutter (dart) since the firebase documentation only addresses Native WEB, iOS, Android etc. but not Flutter. The documentation of cloud_firestore is also way too short.There is only one example that shows how to query multiple documents into a stream which is not what i want to do.

Related issue on missing documentation: https://github.com/flutter/flutter/issues/14324

It can't be that hard to get data from a single document.

UPDATE:

Firestore.instance.collection('COLLECTION').document('ID')
.get().then((DocumentSnapshot) =>
      print(DocumentSnapshot.data['key'].toString());
);

is not executed.

but that does not seem to be correct syntax.

It is not the correct syntax because you are missing a collection() call. You cannot call document() directly on your Firestore.instance . To solve this, you should use something like this:

var document = await Firestore.instance.collection('COLLECTION_NAME').document('TESTID1');
document.get() => then(function(document) {
    print(document("name"));
});

Or in more simpler way:

var document = await Firestore.instance.document('COLLECTION_NAME/TESTID1');
document.get() => then(function(document) {
    print(document("name"));
});

If you want to get data in realtime, please use the following code:

Widget build(BuildContext context) {
  return new StreamBuilder(
      stream: Firestore.instance.collection('COLLECTION_NAME').document('TESTID1').snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return new Text("Loading");
        }
        var userDocument = snapshot.data;
        return new Text(userDocument["name"]);
      }
  );
}

It will help you set also the name to a text view.

If you want to use a where clause

await Firestore.instance.collection('collection_name').where(
    FieldPath.documentId,
    isEqualTo: "some_id"
).getDocuments().then((event) {
    if (event.documents.isNotEmpty) {
        Map<String, dynamic> documentData = event.documents.single.data; //if it is a single document
    }
}).catchError((e) => print("error fetching data: $e"));

Null safe code (Recommended)

You can either query the document in a function (for example on press of a button) or inside a widget (like a FutureBuilder ).

  • In a method: (one time listen)

     var collection = FirebaseFirestore.instance.collection('users'); var docSnapshot = await collection.doc('doc_id').get(); if (docSnapshot.exists) { Map<String, dynamic>? data = docSnapshot.data(); var value = data?['some_field']; // <-- The value you want to retrieve. // Call setState if needed. }
  • In a FutureBuilder (one time listen)

     FutureBuilder<DocumentSnapshot<Map<String, dynamic>>>( future: collection.doc('doc_id').get(), builder: (_, snapshot) { if (snapshot.hasError) return Text ('Error = ${snapshot.error}'); if (snapshot.hasData) { var data = snapshot.data!.data(); var value = data!['some_field']; // <-- Your value return Text('Value = $value'); } return Center(child: CircularProgressIndicator()); }, )
  • In a StreamBuilder : (always listening)

     StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>( stream: collection.doc('doc_id').snapshots(), builder: (_, snapshot) { if (snapshot.hasError) return Text('Error = ${snapshot.error}'); if (snapshot.hasData) { var output = snapshot.data!.data(); var value = output!['some_field']; // <-- Your value return Text('Value = $value'); } return Center(child: CircularProgressIndicator()); }, )

This is simple you can use a DOCUMENT SNAPSHOT

DocumentSnapshot variable = await Firestore.instance.collection('COLLECTION NAME').document('DOCUMENT ID').get();

You can access its data using variable.data['FEILD_NAME']

Update FirebaseFirestore 12/2021

StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection('YOUR COLLECTION NAME')
              .doc(id) //ID OF DOCUMENT
              .snapshots(),
        builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return new CircularProgressIndicator();
        }
        var document = snapshot.data;
        return new Text(document["name"]);
     }
  );
}

This is what worked for me in 2021

      var userPhotos;
      Future<void> getPhoto(id) async {
        //query the user photo
        await FirebaseFirestore.instance.collection("users").doc(id).snapshots().listen((event) {
          setState(() {
            userPhotos = event.get("photoUrl");
            print(userPhotos);
          });
        });
      }

Use this code when you just want to fetch a document from firestore collection , to perform some operations on it, and not to display it using some widget (updated jan 2022 )

   fetchDoc() async {

   // enter here the path , from where you want to fetch the doc
   DocumentSnapshot pathData = await FirebaseFirestore.instance
       .collection('ProfileData')
       .doc(currentUser.uid)
       .get();

   if (pathData.exists) {
     Map<String, dynamic>? fetchDoc = pathData.data() as Map<String, dynamic>?;
     
     //Now use fetchDoc?['KEY_names'], to access the data from firestore, to perform operations , for eg
     controllerName.text = fetchDoc?['userName']


     // setState(() {});  // use only if needed
   }
}

Simple way :

StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection('YOUR COLLECTION NAME')
              .doc(id) //ID OF DOCUMENT
              .snapshots(),
        builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return new CircularProgressIndicator();
        }
        var document = snapshot.data;
        return new Text(document["name"]);
     }
  );
}

Use this simple code:

Firestore.instance.collection("users").document().setData({
   "name":"Majeed Ahmed"
});
        var  document = await FirebaseFirestore.instance.collection('Users').doc('CXvGTxT49NUoKi9gRt96ltvljz42').get();
        Map<String,dynamic>? value = document.data();
        print(value!['userId']);

You can get the Firestore document by following code:

future FirebaseDocument() async{

    var variable = await FirebaseFirestore.instance.collection('Collection_name').doc('Document_Id').get();
    print(variable['field_name']); 
}

There are 2 ways:

1. StreamBuilder

When you want to listen to changes constantly, and want the data to get updated without hot reload/restart

2. FutureBuilder

When you want to get the document only once and have no requirement of listening constantly to the change of the document.


StreamBuilder

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
          stream:  FirebaseFirestore
                   .instance
                   .collection('users')
                   .doc(FirebaseAuth.instance.currentUser!.uid) // 👈 Your document id change accordingly
                   .snapshots(), 
          builder:
              (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
            if (snapshot.hasError) {
              return const Text('Something went wrong');
            }
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Text("Loading");
            }
            Map<String, dynamic> data =
                snapshot.data!.data()! as Map<String, dynamic>;
            return Text(data['fullName']); // 👈 your valid data here
          },
        ),
      ),
    );
  }

FutureBuilder

 @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: FutureBuilder<DocumentSnapshot<Map<String, dynamic>>>(
        future: FirebaseFirestore.instance
            .collection('users')
            .doc(FirebaseAuth.instance.currentUser!.uid)  // 👈 Your document id change accordingly
            .get(),
        builder: (_, snapshot) {
          if (snapshot.hasError) return Text('Error = ${snapshot.error}');
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Text("Loading");
          }
          Map<String, dynamic> data = snapshot.data!.data()!;
          return Text(data['fullName']); //👈 Your valid data here
        },
      )),
    );
  }

Also refer: How to use StreamBuilder and FutureBuilder for single and multiple documents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM