Class 'QuerySnapshot' has no instance getter 'doc'

119 Views Asked by At

I need your help. Here is the scenario I am trying to implement.

  1. A user registers for the app
  2. After the user is successfully registered they need to associate themselves with an agency. The new user is taken to the Agency page. On the page is a DropdownButton that is populated with existing agencies. This DropdownButton is populated from a Firestore document.

I can get the data from the document using a Stream but I can't get it to populate the DropdownButton.

  final _db = FirebaseFirestore.instance;

 body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
Container(
                  child: StreamBuilder(
                    
                      stream: _db.collection('agency').snapshots(),
                      builder: (BuildContext context, AsyncSnapshot snapshot) {
                        if (!snapshot.hasData) {
                          return Center(
                            child: CircularProgressIndicator(),
                          );
                        } else {
                          var length = snapshot.data.doc.length;
                          print('length: ' + length);

                          return new DropdownButton<String>(
                            hint: new Text("Select Agency"),
                            value: _currentAgency,
                     
                            items: snapshot.data.doc.map((Map map) {
                              return new DropdownMenuItem<String>(
                                value: map["name"].toString(),
                                child: new Text(
                                  map["name"],
                                ),
                              );
                            }).toList(),
                          );
                        }
                        ;
                      }),
                ),

I get the following error in the log

======== Exception caught by widgets library ======================================================= The following NoSuchMethodError was thrown building StreamBuilder(dirty, state: _StreamBuilderBaseState<QuerySnapshot, AsyncSnapshot>#4fe22): Class 'QuerySnapshot' has no instance getter 'doc'. Receiver: Instance of 'QuerySnapshot' Tried calling: doc

The relevant error-causing widget was: StreamBuilder file:///C:/Users/nkane/AndroidStudioProjects/tonnah/lib/screens/agency_screen.dart:190:26 When the exception was thrown, this was the stack: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 _AgencyScreenState.build. (package:tonnah/screens/agency_screen.dart:199:54) #2 StreamBuilder.build (package:flutter/src/widgets/async.dart:545:81) #3 _StreamBuilderBaseState.build (package:flutter/src/widgets/async.dart:124:48) #4 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27) ...

E/flutter (13608): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null. E/flutter (13608): Receiver: null E/flutter (13608): Tried calling: E/flutter (13608): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) E/flutter (13608): #1 _AgencyScreenState.getCurrentAgencyProfile (package:tonnah/screens/agency_screen.dart:96:62) E/flutter (13608): E/flutter (13608):

I have been reading a lot of posts and following what they say but I can't get this to work.

0

There are 0 best solutions below