0
votes

enter image description here[enter image description here][2] Future _getName() async{ return await Firestore.instance.collection("admin_details").document(_username).get(); }

FutureBuilder<DocumentSnapshot>(
                  future: _getName(),
                  builder: (context,snapshot) {
                    if (snapshot.connectionState == ConnectionState.done) {
                      if (snapshot.hasData) {
                        if (snapshot.data != null) {
                          return Text(snapshot.data['first_name']);
                        }
                        else {
                          return Text("Loading...");
                        }
                      }

                      else {
                        return Text("No Data");
                      }
                    }
                    else{
                      return null;
                    }
                  }
              ),

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════ The following NoSuchMethodError was thrown building FutureBuilder(dirty, state: _FutureBuilderState#8f31b): The method '[]' was called on null. Receiver: null Tried calling:

The relevant error-causing widget was: FutureBuilder file:///D:/Flutter/ecoleami1_0/lib/StudentActivity.dart:52:28 When the exception was thrown, this was the stack:

0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)

1 DocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:31:42)

2 _StudentActivityPageState.build. (package:ecoleami1_0/StudentActivity.dart:57:50)

3 _FutureBuilderState.build (package:flutter/src/widgets/async.dart)

4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4334:27)

... ════════════════════════════════════════════════════════════════════════════════════════════════════

Can anyone help me, please?

3
i get data after hot reloading applicationSH4LIN
add your firestore to the questionPeter Haddad
Image added of my Firestore structureSH4LIN

3 Answers

-1
votes

Try this

StreamBuilder(
            stream: Firestore.instance
                .collection("admin_details")
                .document('name of the document')
                .snapshots(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                var username = snapshot.data;
                return Text(username['first_name']);
              }
            },
          ),
0
votes

I’m on phone now, so am not 100% sure, but this might be, because your code returns null when the connection state is other than done so:

Replace the ‘return null’ line with return Text(‘loading’);

0
votes

I guess the error is caused by bad state management in the app..