Home > Enterprise >  The method 'firstWhere' isn't defined for the type 'Object' in flutter
The method 'firstWhere' isn't defined for the type 'Object' in flutter

Time:02-10

Here is the code in which I'm getting two errors. The first error I am getting is- "The method 'firstWhere' isn't defined for the object in flutter" and second one is-"The argument type 'Object?' can't be assigned to the parameter type 'List'"

I am new in fetching data from api and working with these things so I am not sure how to shorten this.

                  FutureBuilder(
                    future: countryList,
                      builder: (context,snapshot){
                      if(snapshot.hasData){
                        return TypeAheadFormField(
                          textFieldConfiguration: TextFieldConfiguration(
                            controller: this._typeAheadController,
                            decoration: InputDecoration(
                              hintText: 'Type here country name',
                              hintStyle: TextStyle(fontSize: 16),
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(15),
                                borderSide: BorderSide(
                                  width: 0,
                                  style: BorderStyle.none,
                                ),
                              ),
                              filled: true,
                              fillColor: Colors.grey[200],
                              contentPadding: EdgeInsets.all(20),
                              prefixIcon: Padding(
                                padding: EdgeInsets.only(left: 24.0, right: 16.0),
                                child: Icon(
                                  Icons.search,
                                  color: Colors.black,
                                  size: 28,
                                ),
                              ),
                            ),
                          ),
                          suggestionsCallback: (pattern) {
                            return _getSuggestions(snapshot.data, pattern);

                          },
                          itemBuilder: (context, suggestion) {
                            return ListTile(
                              title: Text(suggestion.toString()),
                            );
                          },
                          transitionBuilder: (context, suggestionsBox, controller) {
                            return suggestionsBox;
                          },
                          onSuggestionSelected: (suggestion) {
                            this._typeAheadController.text = suggestion.toString();
                        setState(() {
                          summaryList = covidService.getCountrySummary(
                              snapshot.data! .firstWhere((element) => element.country == suggestion).slug);

                        });
                          },
                        );
                      }
                      else return Text("Loading...");
                      }
                      ),
                    FutureBuilder(
                      future: summaryList,
                      builder: (context,snapshot){
                           if(snapshot.hasData){
                              return Final_Data(summaryList:snapshot.data);
                           }
                           else if(snapshot.connectionState==ConnectionState.active){
                             return Text("Connections state");
                           }
                           else if(snapshot.hasError){
                             return Text("Error");
                           }
                           else return Text("Eroro adl");
                      },
                    ),

Error...

The method 'firstWhere' isn't defined for the type 'Object'.
The argument type 'Object?' can't be assigned to the parameter type 'List<CountrySummaryModel>'.

CodePudding user response:

Have you tried using generics? Give a specific type to your FutureBuilder like:

FutureBuilder<List<String>>(
// your code here
);
  • Related