0

I am a few weeks into a flutter. I like it a lot but I have an issue that I don't understand. I used the approach below for a drop drown. It works just fine in the dartpad but Visio throws an error and I am not sure what to do about it. Please help.

Error:

Object? newValue
A value of type 'Object?' can't be assigned to a variable of type 'String'. Try changing the type of the variable, or casting the right-hand type to 'String'.dartinvalid_assignment

Code:

DropdownButton(
   hint: Text('Pick a category'),
   value: _selectedCategory,
   onChanged: (newValue) {
      setState(() {
         _selectedCategory = newValue;
      });
   },
   items: _categories.map((category) {
      return DropdownMenuItem(
             child: new Text(category),
             value: category,
      );
    }).toList(),
 ),
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

2 Answers2

0
DropdownButton(
              hint: Text("State"),
              value: "Gujarat",
              items: [
                ...statelist
                    .map((e) =>
                        DropdownMenuItem<String>(value: e, child: Text(e)))
                    .toList(),
              ],
              
            )
0
DropdownButtonHideUnderline(
                                  child: DropdownButton(
                                      icon: Icon(Icons.arrow_drop_down_sharp,color: appStyle.Colors('white'),),
                                      iconSize: 30,
                                      items: ProductCategories.map((dynamic map){
                                        return new DropdownMenuItem<String>(
                                          value: map['Id'].toString(),
                                          child: new Text(map['Name'],
                                              style: new TextStyle(color:  appStyle.Colors('primaryTextTheme'))),
                                        );
                                      }).toList(),
                                      onChanged: (value) {
                                        setState(() {
                                          selectCategoryName(ProductCategories,value);
                                        }
                                        );
                                      }
                                  )),

this is the code for dropdown in my project , Feel free to question about it if you use it

  • List ProductCategories=[]; _analyzeCategories(value){ print(appStyle.showDebug('WarnLog', value)); for(var counter=0;counter < value.length;counter++){ try { ProductCategories.add({'Id':value[counter]['_id'],'Name':value[counter]['Name']}); } catch (e) { print('eerrr--> $e'); } } } _analyzeCategories("YourData"), – saeed bayat Aug 28 '21 at 13:50
  • I have used it like this you can change it to your keys – saeed bayat Aug 28 '21 at 13:51