Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

 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: () {})
          ],
        ),
like image 369
Mm Victory Avatar asked May 28 '19 23:05

Mm Victory


4 Answers

You should check null safe

Text(cart_prod_qty??'default value'),
like image 174
Nikhil Vadoliya Avatar answered Nov 16 '22 01:11

Nikhil Vadoliya


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()
like image 45
Praneeth Dhanushka Fernando Avatar answered Nov 16 '22 02:11

Praneeth Dhanushka Fernando


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'),
like image 5
Jitesh Mohite Avatar answered Nov 16 '22 03:11

Jitesh Mohite


An advice for you in this case and all who are reading this.

When you add an non direct String value to the Text() don't directly insert it like you did in this example:

Text(cart_prod_qty)

Because you can get "A non-null String must be provided to a Text widget" like I did.

So I advise you to practice this in the future it's more safe:

Text('${cart_prod_qty}')
like image 4
NiiTii Avatar answered Nov 16 '22 03:11

NiiTii