Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Error: Couldn't infer type parameter 'T'

Tags:

flutter

dart

I'm working on Flutter 2.2.1 (channel stable). I reccently changed my SDK's environment from 2.7.0 to 2.12.0 in order to add plugins and I got a lot of errors in my code. One of them is about a list of radio buttons.

My code :

ListView.builder(
    physics: NeverScrollableScrollPhysics(),
    shrinkWrap: true,
    itemCount: languages.length,
    itemBuilder: (ctx, index) {
      return RadioListTile(
        title: Row(
          children: [
            Image.asset(
              'icons/flags/png/${languages[index]['flag']}.png',
              package: 'country_icons',
              height: 16,
            ),
            Text('  ' + languages[index]['language']),
          ],
        ),
        value: languages[index]['code'],
        groupValue: widget.languageChosen,
        onChanged: (_languageSelected) {
          setState(() {
            _languageChosen = _languageSelected;
            widget.onLanguageChange!(_languageChosen);
          });
        },
      );
    }),

The error is about 'RadioListTile' and the message is:

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 'dynamic'. Parameter 'groupValue' declared as 'T?' but argument is 'dynamic'.

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

I got another error about '_languageSelected' at the line _languageChosen = _languageSelected; The error is:

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'.

But I don't know if both errors are linked.

like image 911
Edouard Delep Avatar asked Mar 04 '26 01:03

Edouard Delep


1 Answers

It was a simple fix for me I forget to specifically declare the type of my values in my widget.

Error

DropdownButton()

No Error

DropdownButton<String>()
like image 115
Zach Gonzalez Avatar answered Mar 06 '26 03:03

Zach Gonzalez