Issue
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.
Solution
Correct code:
ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: languages.length,
itemBuilder: (ctx, index) {
return RadioListTile<String>(
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.toString();
widget.onLanguageChange!(_languageChosen);
});
},
);
}),
Answered By - Edouard Delep
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.