Null Safety is available in the flutter beta channel, starting with build 1.24.0-10.2.pre. You can now migrate your flutter packages to use non-nullable types. So now, we will learn how to solve a Non-Null String Must be provided to a Text Widget in flutter.
How to Solve a Non-Null String Must be Provided to a Text Widget In Flutter ??
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.
So you should check null safe
assert( data != null, 'A non-null String must be provided to a Text widget.', ),
To solve the above error you have to provide some default text.
new Text(cart_prod_qty!=null?cart_prod_qty:'Default Value'),
Just check for null and give a default
So you can keep it empty if you wish
Or else you can make text widget optional
cart_prod_qty!=null? Text(cart_prod_qty): Container()
So The value may be empty therefore you’re getting a null error try this if its an optional field:
new Text(cart_prod_qty == null ? '' : cart_prod_qty),
This looks like a problem with a null value. In-Text(cart_prod_qty), you’re providing null to a Text widget, which is not allowed. in the Text widget, The data parameter must not be null.
The solution does not pass null to Text widgets.
To avoid it set a default value or check the values you are receiving are not null.
when calling the Text() widgets
So 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 the null value and pass a default value like this.
ListTile( title: Text(User[‘user_name’] ?? 'default'), subtitle: Text(User[‘user’_info] ?? 'default'), );
Thanks for being with us on a Flutter Journey !!!
Also, Don’t forget to look at How to Make Copyable Text Widget In Flutter?
Keep Learning !!! Keep Fluttering !!!
So in this article,
we have learned how to solve a Non-Null String Must be provided to a Text Widget in flutter.
Drop us your valuable suggestion/feedback to serve you better.
FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc.
FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.