Home > Software engineering >  Null check operator error when trying to read Passed variable in Constructor
Null check operator error when trying to read Passed variable in Constructor

Time:07-23

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.

CodePudding user response:

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);
  }

CodePudding user response:

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));
  }
}
  • Related