0

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.

LostTexan
  • 431
  • 5
  • 18
  • isn't `doc` supposed to be `docs`? – Alex Hartford May 11 '21 at 17:06
  • Seems like the wrong lane is... var length = snapshot.data.doc.length; Like can't access to "doc" Try put a breakpoint there and check if exists "doc". Because seems like "doc" doesn't exist. And all examples i've found they are using "data" directly – Dani Roman May 11 '21 at 17:06
  • Thanks for your responses. I changed doc to docs. snapshot.data.docs does have data. There are 2 records which is what I expect. At the line if(!snapshot.hasData) is true so when I proceed from there I go to object_patch.dart and line throw new NoSuchMethodError.withInvocation(this, invocation); – LostTexan May 11 '21 at 18:31
  • Now the error message is: The following _TypeError was thrown building StreamBuilder(dirty, state: _StreamBuilderBaseState>#643a6): type '(Map) => DropdownMenuItem' is not a subtype of type '(QueryDocumentSnapshot) => dynamic' of 'f' – LostTexan May 11 '21 at 18:38

0 Answers0