I am trying to figure out if widget.onChanged
of RadioListTile<T>
is not null, but it seems like dart is very strict when it comes to generics, any suggestion on how I can check it?
(I am a little new to Fluter/Dart )
void testWidgetOnNUllDynamic(dynamic clbk) {
if (clbk != null) {
print('not null');
}
}
void testWidgetOnNUllCallback(Widget widget) {
if (widget is RadioListTile) {
// if (widget.onChanged != null) {
// //type '(String?) => void' is not a subtype of type '((dynamic) => void)?'
// print('Crash here');
// }
//attempt to erase the type
testWidgetOnNUllDynamic(widget.onChanged);
}
}
//This method is redundant
void testIfOnChangeIsNull(Widget widget) {
testWidgetOnNUllCallback(widget);
}
test('when widget is RadioListTile onChange test', () {
// <String> - Can be anything
RadioListTile<String> widget = RadioListTile(value: 'value', activeColor: 0, toggleable: true, onChanged:(value) => debugPrint('Test'), groupValue: null,);
testIfOnChangeIsNull(widget);
});
I think that I understand the error I am not sure how to avoid it OR how to check of nallability without casting it to concrete type
Asked same question here
Thanks
I am trying to figure out if widget.onChanged
of RadioListTile<T>
is not null, but it seems like dart is very strict when it comes to generics, any suggestion on how I can check it?
(I am a little new to Fluter/Dart )
void testWidgetOnNUllDynamic(dynamic clbk) {
if (clbk != null) {
print('not null');
}
}
void testWidgetOnNUllCallback(Widget widget) {
if (widget is RadioListTile) {
// if (widget.onChanged != null) {
// //type '(String?) => void' is not a subtype of type '((dynamic) => void)?'
// print('Crash here');
// }
//attempt to erase the type
testWidgetOnNUllDynamic(widget.onChanged);
}
}
//This method is redundant
void testIfOnChangeIsNull(Widget widget) {
testWidgetOnNUllCallback(widget);
}
test('when widget is RadioListTile onChange test', () {
// <String> - Can be anything
RadioListTile<String> widget = RadioListTile(value: 'value', activeColor: 0, toggleable: true, onChanged:(value) => debugPrint('Test'), groupValue: null,);
testIfOnChangeIsNull(widget);
});
I think that I understand the error I am not sure how to avoid it OR how to check of nallability without casting it to concrete type
Asked same question here
Thanks
2 Answers
Reset to default 0The answer came from here The easiest work around was to do something like that:
void testWidgetOnNUllCallback(Widget widget) {
if (widget is RadioListTile) {
if ((widget as dynamic).onChanged != null) {
print('wont crash any more');
}
}
}
More details about this situation can be found here and here.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744912077a4600612.html
if (widget is RadioListTile<String>) { ... }
if without the<String>
, thenwidget
will be type promoted toRadioListTile<dynamic>
. – mmcdon20 Commented Mar 7 at 22:56if (widget is RadioListTile)
and get the error:type '(String?) => void' is not a subtype of type '((dynamic) => void)?'
so it's not promoted todynamic
. TheT
can be anything, I need to check ifonChanged
is not null – Mike.R Commented Mar 8 at 0:58RadioListTile
is short forRadioListTile<dynamic>
. Mouse over any use ofwidget
inside the body of the if condition, and the editor will show that the type has been promoted toRadioListTile<dynamic>
in the tooltip. – mmcdon20 Commented Mar 8 at 1:35