简体   繁体   中英

How to retreive data from firestore flutter

I'm new into flutter and firebase integrations and I'm having some troubles to retreive all the data from the firebase collection.

I have tried this method:

    getCollection() {
      CollectionReference coleccion =
          FirebaseFirestore.instance.collection('materias');
      return Container(
        child: StreamBuilder(
          stream: coleccion.doc('aprobadas').snapshots(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState == ConnectionState.active) {
              return Text(snapshot.data.data()['codigo'],
                  style: TextStyle(fontSize: 50, color: Colors.white));
            } else {
              return CircularProgressIndicator();
            }
          },
        ),
      );
    }

Now I'm a little bit frustrated because I have tried a differents methods and doesn't work.

I really appreciate all the help.

Best regards

Data can be retrieved using the below code from firestore to flutter.

One-time Read

call the Query.get or DocumentReference.get methods

class GetUserName extends StatelessWidget {
  final String documentId;

  GetUserName(this.documentId);

  @override
  Widget build(BuildContext context) {
    CollectionReference users = FirebaseFirestore.instance.collection('users');

    return FutureBuilder<DocumentSnapshot>(
      future: users.doc(documentId).get(),
      builder:
      (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {

        if (snapshot.hasError) {
          return Text("Something went wrong");
        }

        if (snapshot.hasData && !snapshot.data!.exists) {
          return Text("Document does not exist");
        }

        if (snapshot.connectionState == ConnectionState.done) {
          Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
          return Text("Full Name: ${data['full_name']}         ${data['last_name']}");
        }

        return Text("loading");
      },
    );
  }
}

Realtime changes

FlutterFire provides support for dealing with real-time changes to collections and documents. A new event is provided on the initial request, and any subsequent changes to collection/document whenever a change occurs (modification, deleted or added).

Both the CollectionReference & DocumentReference provide a snapshots() method which returns a Stream:

Stream collectionStream = FirebaseFirestore.instance.collection('users').snapshots();
Stream documentStream = FirebaseFirestore.instance.collection('users').doc('ABC123').snapshots();

Please refer official documentation here

You can use a StreamBuilder . That will be easy to understand.

StreamBuilder(
    stream: FirebaseFirestore.instance.collection("collection").snapshot,
    builder: (BuildContext context,snapshot) {
        if(snapshot.hasdata!=true) {
            return CircularProgressIndicator();
        } else {
            return ListView.builder(
                itemcount:snapshot.data.docs.length,
                builder(context,index) {
                return Text(snapshot.data.docs[index].data()["filedname"]);
        }
    }
)

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