1

I'm trying to read value passed from a Stateful object to another Stateful object. I've defined a simple String variable, and trying to read the value in the constructor. It's showing error: Null check operator used on a null value.

But when I try to read the value in the widget, it works, and doesn't show any error.

Here is the main.dart

void main() {
 runApp(MaterialApp(
  home: HomePage(),
 ));
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  var dummyText = "Dummy Text";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => DPage(dummyText)));
            },
            child: Text('Press Me!'),
          ),
        ));
  }
}

Here is the DPage where I'm trying to read variable dummyText.

class DPage extends StatefulWidget {
  var dvariable;
  DPage(this.dvariable);
  @override
  State<DPage> createState() => _DPageState();
}

class _DPageState extends State<DPage> {
  _DPageState() {
     print(widget.dvariable);
  }

  @override
  Widget build(BuildContext context) {
    return Container(child: Text(widget.dvariable));
  }
}

if I comment the following line, which is in constructor -

 print(widget.dvariable); 

It runs without any problem. But if I try to access the widget.dvariable in the constructor, it throws error -

 Null check operator used on a null value

How do I access this dvariable or value of "dummyText" in constructor? Any pointers will be greatly appreciated. Note: I'm noob in dart/flutter.

2 Answers 2

1

You can use initState for this case. Constructor gets called before initState State and not ready to read widget class variables.

  @override
  void initState() {
    super.initState();
    print(widget.dvariable);
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! @Yeasin Sheikh are you essentially saying we can do all the initialization that we usually do in constructor, it's better to do that in initState?
Yes, initState call when the state it creates. You can do all sync operation here. & it is recommended to use initState. You can find more here
0
class DPage extends StatefulWidget {
  var dvariable;
  DPage(this.dvariable);
  @override
  State<DPage> createState() => _DPageState();
}

class _DPageState extends State<DPage> {
  
 @override
  void initState() {
    super.initState();
    print(widget.dvariable);
  }

  @override
  Widget build(BuildContext context) {
    return Container(child: Text(widget.dvariable));
  }
}

Or

class DPage extends StatefulWidget {
  var dvariable;
  DPage(this.dvariable);
  @override
  State<DPage> createState() => _DPageState();
}

class _DPageState extends State<DPage> {
  _DPageState() {
     print(widget.dvariable);
  }
  
 @override
  void initState() {
    super.initState();
    _DPageState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(child: Text(widget.dvariable));
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.