flutterdropdownbox

Flutter DropdownButton() issue


I have issues with my DropdownButton() in flutter it gives me this error. I don't know what is wrong here....

Couldn't infer type parameter 'T'.
Tried to infer 'dynamic' for 'T' which doesn't work:
Parameter 'onChanged' declared as     'void Function(T?)?'
                    but argument is 'void Function(Object?)'.
The type 'dynamic' was inferred from:
Parameter 'items' declared as     'List<DropdownMenuItem<T>>?'
                but argument is 'List<DropdownMenuItem<dynamic>>'.

Consider passing explicit type argument(s) to the generic.

This is the snippet of my DropdownButton code.

DropdownButton(
                    items: [
                      DropdownMenuItem(
                        child: Text(
                          'Solar System',
                          style: TextStyle(
                            fontFamily: 'Avenir',
                            fontSize: 24,
                            color: const Color(0x7cdbf1ff),
                            fontWeight: FontWeight.w500,
                          ),
                          textAlign: TextAlign.left,
                        ),
                      ),
                    ],
                    onChanged: (value) {},
                    icon: Padding(
                      padding: const EdgeInsets.only(left: 16.0),
                      child: Image.asset('assets/drop_down_icon.png'),
                    ),
                    underline: SizedBox(),
                  ),

Solution

  • You have to specify the type <String> next to DropdownButton and DropdownMenuItem

    DropdownButton<String>(
     items: [
      DropdownMenuItem<String>(
       child: Text(
        'Solar System',
        style: TextStyle(
         fontFamily: 'Avenir',
         fontSize: 24,
         color: const Color(0x7cdbf1ff),
         fontWeight: FontWeight.w500,
        ),
        textAlign: TextAlign.left,
       ),
      ),
     ],
     onChanged: (value) {},
     icon: Padding(
      padding: const EdgeInsets.only(left: 16.0),
      child: Image.asset('assets/drop_down_icon.png'),
     ),
     underline: SizedBox(),
    ),