Couldn't infer type parameter 'T'

Viewed 3010

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 'value' declared as 'T?' but argument is 'String'. Parameter 'items' declared as 'List<DropdownMenuItem>?' but argument is 'List<DropdownMenuItem>'.

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

I have DropDownformfield which shows above problem The code is

SizedBox(
                    width: deviceWidth * .9,
                    height: deviceHeight * 0.15,
                    child: DropdownButtonFormField(
                      value: _dropDownValue,
                      dropdownColor: Colors.blueAccent,
                      iconSize: deviceWidth * .05,
                      icon: Icon(
                        Icons.arrow_drop_down_circle,
                        color: Colors.white,
                      ),
                      style: TextStyle(
                        color: Colors.black,
                        fontFamily: 'Constantia',
                        fontSize: 18.0,
                      ),
                      autovalidateMode: AutovalidateMode.always,
                      decoration: InputDecoration(
                        enabled: true,
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: Colors.red,
                            width: 2,
                            style: BorderStyle.solid,
                          ),
                        ),
                      ),
                      onChanged: (value) {
                        setState(() {
                          _dropDownValue = value.toString();
                          print(_dropDownValue);
                        });
                      },
                      items: <DropdownMenuItem<dynamic>>[
                        menuItem('none'),
                        menuItem('10'),
                        menuItem('20'),
                        menuItem('30'),
                        menuItem('40'),
                        menuItem('50'),
                        menuItem('60'),
                        menuItem('70'),
                        menuItem('80'),
                        menuItem('90'),
                        menuItem('100'),
                      ],
                    ),
                  ),

And widget as

DropdownMenuItem menuItem(String val) {
    final deviceWidth = MediaQuery.of(context).size.width;
    return DropdownMenuItem<String>(
      value: val,
      child: Container(
        width: deviceWidth * 0.6,
        child: Text(
          val,
          style: TextStyle(
            fontFamily: 'Constantia',
            fontSize: 18.0,
            color: Colors.white,
          ),
        ),
      ),
    );

The Code works fine but Android Studio displays this as a problem.

3 Answers

It is just intellisense suggesting you to provide generic types in your code.

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

Since you are providing all String values to menuItems. It is better to provide the generic type when you call them.

items : <DropdownMenuitem<String>>[
...
...
]

It is better to provide generic types to avoid any bug or error in code. since you have mentioned DropDownMenuItem in your menuItem function android studio is giving you a warning that you have provided dynamic here.

items: <DropdownMenuItem<dynamic>>[      // ==> change dynamic to String to solve that warning
                        menuItem('none'),
                        menuItem('10'),
                        menuItem('20'),
                        menuItem('30'),
                        menuItem('40'),
                        menuItem('50'),
                        menuItem('60'),
                        menuItem('70'),
                        menuItem('80'),
                        menuItem('90'),
                        menuItem('100'),
                      ],

I guess halfway through your code you forgot what your types are... add them back and it will work:

items: <DropdownMenuItem<String>>[
                        menuItem('none'),
                        menuItem('10'),
                        menuItem('20'),
                        menuItem('30'),
                        menuItem('40'),
                        menuItem('50'),
                        menuItem('60'),
                        menuItem('70'),
                        menuItem('80'),
                        menuItem('90'),
                        menuItem('100'),
                      ],

And...

DropdownMenuItem<String> menuItem(String val) {
    final deviceWidth = MediaQuery.of(context).size.width;
    return DropdownMenuItem<String>(
      value: val,
      child: Container(
        width: deviceWidth * 0.6,
        child: Text(
          val,
          style: TextStyle(
            fontFamily: 'Constantia',
            fontSize: 18.0,
            color: Colors.white,
          ),
        ),
      ),
    );
Related