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

谁都会走 提交于 2020-12-01 09:44:12

问题


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 code?

 trailing: Container(
        height: 60,
        width: 60,
        padding: EdgeInsets.only(left: 10),
        child: Column(
          children: <Widget>[
            new GestureDetector(child: Icon(Icons.arrow_drop_up), onTap: () {}),
            new Text(cart_prod_qty),
            new GestureDetector(child: Icon(Icons.arrow_drop_down), onTap: () {})
          ],
        ),

回答1:


You should check null safe

Text(cart_prod_qty??'default value'),



回答2:


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'),



回答3:


Just check for null and give a default

Text(cart_prod_qty!=null?cart_prod_qty:'default value'),

You can keep it empty if you wish

Text(cart_prod_qty!=null?cart_prod_qty:''),

Or else you can make text widget optional

cart_prod_qty!=null? Text(cart_prod_qty): Container()



回答4:


The value may be empty therefore youre getting a null error try this if its an optional field:

new Text(cart_prod_qty == null ? '' : cart_prod_qty),



回答5:


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'),
);



回答6:


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 : '')




回答7:


just set Text widget value optional like

Text(value ?? ""),



回答8:


I GOT SAME ERROR DUE TO THIS

BEFORE:

title:  Text(widget.title)

AFTER:

title:  Text('ram')

THIS SOLVED MY ERROR

To solve this error Add Double quote or single quote title: Text('ram')



来源:https://stackoverflow.com/questions/56351386/a-non-null-string-must-be-provided-to-a-text-widget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!