A non-null String must be provided to a Text widget

前端 未结 9 1679
醉酒成梦
醉酒成梦 2021-02-13 20:15

I\'m trying to add the option for the quantity to be adjusted but I get an error saying \"A non-null String must be provided to a Text widget\" How do I provide this, to this co

相关标签:
9条回答
  • 2021-02-13 21:00

    The problem is visible. You're passing null to Text widget. new Text(cart_prod_qty) This can be due to delay of response from api (if you're using it). Or there is no value in the variable "cart_prod_qty". You can handle it like this: new Text(cart_prod_qty != null ? cart_prod_qty.toString : '')

    0 讨论(0)
  • 2021-02-13 21:02

    The error itself shows what's wrong in the code, Text widget works only with string and for null they intentionally have thrown an exception. Check text.dart file implementation where they added throwing an exception.

    assert(
             data != null,
             'A non-null String must be provided to a Text widget.',
           ),
    

    To solve above error you have to provide some default text.

    new Text(cart_prod_qty!=null?cart_prod_qty:'Default Value'),
    
    0 讨论(0)
  • 2021-02-13 21:09

    This looks like a problem with null value. In Text(cart_prod_qty), you're providing null to a Text widget, which is not allowed. in Text widget The data parameter must not be null.

    The solution Do not pass null to Text widgets. To avoid it set a default value or Check the values you are receiving is not null. when calling the Text() widgets

    You can assign a default value and could change that to follow:

     

    Text(cart_prod_qty  ?? 'default value').
    

    If you have a dart model or collection type then check null value and pass a default value like this

    ListTile(
        title: Text(User[‘user_name’] ?? 'default'),
        subtitle: Text(User[‘user’_info] ?? 'default'),
    );
    
    0 讨论(0)
提交回复
热议问题