flutter - Crash type casting issues in Dart related to generic values and null checks, type '(String?) => void&am

I am trying to figure out if widget.onChanged of RadioListTile<T> is not null, but it seems like

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

Share edited Mar 9 at 14:47 Mike.R asked Mar 7 at 19:31 Mike.RMike.R 2,9462 gold badges30 silver badges37 bronze badges 4
  • if (widget is RadioListTile<String>) { ... } if without the <String>, then widget will be type promoted to RadioListTile<dynamic>. – mmcdon20 Commented Mar 7 at 22:56
  • In my example I check if (widget is RadioListTile) and get the error: type '(String?) => void' is not a subtype of type '((dynamic) => void)?' so it's not promoted to dynamic. The T can be anything, I need to check if onChanged is not null – Mike.R Commented Mar 8 at 0:58
  • RadioListTile is short for RadioListTile<dynamic>. Mouse over any use of widget inside the body of the if condition, and the editor will show that the type has been promoted to RadioListTile<dynamic> in the tooltip. – mmcdon20 Commented Mar 8 at 1:35
  • any chance you can try to run what you suggesting and post an answer? Maybe I am missing something – Mike.R Commented Mar 8 at 3:46
Add a comment  | 

2 Answers 2

Reset to default 0

The 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信