How to fix 'Text is null' in flutter
Asked Answered
D

3

7

I want to create an app which has a TabBarView with two tabs. On the first Tab there is a Textfield and on the other tab there is a text widget which should display the text which you entered into Textfield but I always get an error because text is null.( I'm new in programming with flutter)

I tried to initialize the variable in TextOutput class but it didn't work because the variable is final.

TabBarView(
      children: <Widget>[
        TextCreatePage(), TextOutput()
      ],




class TextCreatePageState extends State<TextCreatePage> {
String textvalue;
@override
Widget build(BuildContext context) {
  return Center(child: TextField(
    onChanged: (String value) {
    setState(() {
       textvalue = value;
       TextOutput(textvalue: textvalue,);
            });





class TextOutput extends StatelessWidget {
final String textvalue;

TextOutput({this.textvalue});
@override
Widget build(BuildContext context) {
    return Text(textvalue); 
}
}
Doley answered 27/1, 2019 at 11:24 Comment(0)
P
15

So, for anyone that search this and got here, I'm using this to manage null String variables.

1. Show Empty Text

String nullText; //for null-safety change to: String? nullText;

//now, inside of your widget build
Text(nullText ?? '');

2. Not show Text Widget

String nullText;

//now, inside of your widget build
if(nullText != null)
 Text(nullText);

with null-safety

String? nullText;

//now, inside of your widget build
if(nullText != null)
 Text(nullText!);

Also you can show like this, but this show the null word

String nullText; //for null-safety change to String? nullText;

//now, inside of your widget build
Text('$nullText');

Live Example https://dartpad.dev/faab5bc3c2df9573c0a75a5ce3d4b4b9

Pylos answered 7/7, 2020 at 4:15 Comment(0)
L
3

It's not clear from the information your provided in your question what code causes the error, but I guess it is this line:

return Text(textvalue); 

If you change it to

return textvalue != null ? Text(textvalue) : Container(); 

your error should go away.

Liveried answered 27/1, 2019 at 13:21 Comment(8)
I think it is a problem with the textvalue variable in Textoutput and the constructor because textvalue always is null.Doley
Dorsn't look this way.Dasyure
Hard to tell from here.Dasyure
Ok, i tested some things and now I know where the error is but I don't know how to fix it. The error is probably at the constructor in the TextCreatePageState class.Doley
The code in your question doesn't contain such a constructor.Dasyure
I meant the TextOutput(textvalue: textvalue);Doley
I don't see anything wrong with it. Should be easy to debug though.Dasyure
When I use Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => TextOutput(textvalue: textvalue2,))); everything works ?Doley
Q
0

Hector Aguero's answer is what I've been doing to workaround the error.

I use this everywhere:

Text(nullText ?? '');

But I wonder what's stopping Flutter team to make Text() widget support null value?

// This should be acceptable and simply not rendering any text
// why they don't allow this is beyond me
Text(nullText);
Queeniequeenly answered 17/2, 2022 at 3:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.