24

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: () {})
          ],
        ),
Mm Victory
  • 372
  • 1
  • 4
  • 18

12 Answers12

43

You should check null safe

Text(cart_prod_qty??'default value'),
Ravinder Kumar
  • 7,407
  • 3
  • 28
  • 54
Nikhil Vadoliya
  • 1,542
  • 10
  • 17
  • And I'd like to add: In flutter, it often makes sense to *not* create a `Text` widget in this case, e.g. in all kinds of `Tiles` and other stuff: `Tile(..., subtitle: cart_prod_qty == null ? null : Text(cart_prod_qty)),` This will render more nicely because it has no empty `Text` widget around. – rgisi Dec 01 '20 at 10:41
  • Helped me understand my dumb mistake. – Gaurav Joseph Feb 07 '21 at 21:20
  • best answer..... – Rahul Kushwaha Jan 11 '22 at 11:28
9

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()
Ravinder Kumar
  • 7,407
  • 3
  • 28
  • 54
5

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'),
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
4

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}')
NiiTii
  • 237
  • 6
  • 25
4

if you know for certain that it's not null, then you can safely do this:

Text(cart_prod_qty!),

to me, when it's definitely not null in the usage case, it's cleaner than doing this:

Text(cart_prod_qty ?? ''),
3

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

If you know that your string is nullable you could do something like this. Although I used the visibility widget because I don't want my widget to be displayed if it's null.

class NullableTextWidget extends StatelessWidget {
      final String? text;
      final TextStyle? textStyle;
      const NullableTextWidget({Key? key, this.text,this.textStyle}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Visibility(
          visible: text != null,
          child: Text(
            text ?? "",
            style: textStyle,),
        );
      }
    }
0

just set Text widget value optional like

Text(value ?? ""),
Akshay I
  • 3,675
  • 1
  • 34
  • 57
0

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

Anas
  • 49
  • 6
0

I faced the same problem while fetching data from Firebase . It was initially: Text(widget.title) - it caused the error .

Later, I transformed the Text() into this format: Text('${widget.title}')

  • this interpolates the fetched data inside the Text() widget .

If you receive a null value return , that would be a server issue .

Hope this might help someone .

Farial Mahmod
  • 61
  • 1
  • 4
-1

I have faced that same problem.... I have resolved it by using a "new" keyword before Text...

child: new Text(mydata[1]["2"][k]),

-2

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

raman raman
  • 1,655
  • 4
  • 22
  • 34