documentSnapshot.data() do not pass to the builder: Flutter

Viewed 565

I have been trying to get a document data from /users collections, but the problem is that after I declare de builder: which is a required parameter for FutureBuiulder, the data is not passing it and I get data = null.
what am I doing wrong?

future: FirebaseFirestore.instance
              .collection('users')
              .doc(uid)
              .get()
              .then((DocumentSnapshot documentSnapshot) {
            if (documentSnapshot.exists) {
              print('The Documents Exists');
              print(documentSnapshot.data());
            }
            else {
              print('The document Do not Exist');
            }
              } //here closes then((DocumentSnapshot documentSnapshot) {
              ),// here closesthen((DocumentSnapshot documentSnapshot)

I get the document right as you can see below on the img
However, the data is not passing after the builder:

builder: (context, documentSnapshot) {
              if (documentSnapshot.hasData) {
                print('The Documents Exists');
                print(documentSnapshot.data());
              }
              else {
                print('The document Do not Exist');
              }

enter image description here

EDIT

Here is my full code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class NewHome extends StatefulWidget {
  @override
  _NewHomeState createState() => _NewHomeState();
}

class _NewHomeState extends State<NewHome> {
  FirebaseFirestore firestore = FirebaseFirestore.instance;
  FirebaseAuth auth = FirebaseAuth.instance;

  @override
  Widget build(BuildContext context) {
    final User user = auth.currentUser;
    final uid = user.uid;
    return new Scaffold(
        body: new Container(
            child: FutureBuilder(
                future: FirebaseFirestore.instance
                    .collection('users')
                    .doc('uid')
                    .get(),


                builder: (context, snapshot) {
                  DocumentSnapshot manuais =
                  snapshot.data;
                  if (snapshot.hasData) {
                    print('okkk');
                  }
                  else{
                    print('nopeeeee');
                  }

            return Container(
                      padding: EdgeInsets.all(16),
                      child: ListView.builder(
                          itemCount: snapshot.data.documents.length,
                          itemBuilder: (context, uid) {
                            return Card(
                                color: Colors.grey[250],
                                child: Container(
                                    padding: EdgeInsets.all(10),
                                    child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          new Image.asset(
                                            'Images/pdflogo.png',
                                            width: 32,
                                          ),
                                          Center(
                                            child: Text(
                                              (snapshot
                                                  .data()['nome']
                                                  .toString()),
                                              maxLines: 1,
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(fontSize: 16),
                                            ),
                                          ),
                                        ])));
                          }));
                })));
  }
}

2 Answers

Don't use then with FutureBuilder

Pass your async method directly to future then only it resolves into snapshot, and you can access different states like hasData, hasError getters.

FutureBuilder(
      future: FirebaseFirestore.instance
          .collection('users')
          .doc("UID")
          .get(),
      builder: (context, snapshot) {
        if(snapshot.hasData){
            /// documentSnapshot.data()
        }
        return Container();
      }
    );

Now im getting Class 'DocumentSnapshot' has no instance getter 'documents'. Receiver: Instance of 'DocumentSnapshot' Tried calling: documents – KeplerDuff Sep 21 at 2:02

Hey guys I got the same error you just have to delete () from snapshot.data**()**['nome'].toString() =>> snapshot.data['nome'].toString() like this, then it works.

Related