Home > OS >  error: Undefined name 'widget' in Dart when trying to build navigation
error: Undefined name 'widget' in Dart when trying to build navigation

Time:10-03

Can someone help me? Newbie with Flutter here.

I have this:

class SecondRoute extends StatelessWidget {
  final Place? place;
  const SecondRoute({Key? key, this.place}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text(widget.place!.text!),
        ),
      ),
    );
  }

And:

class PlaceDetail extends StatefulWidget {
  final Place? place;

  PlaceDetail({Key? key, this.place}) : super(key: key);

  @override
  _PlaceDetailState createState() {
    return _PlaceDetailState();
  }
}

I successfully can navigate to the next page, but the problem is with this part:

child: Text(widget.place!.text!)

I am trying to show some content that I fetch with JSON. In a different widget, that specific widget.place!.text! works like a charm, but in this class and widget, I can't get it to work and t thus I get the error:

error: Undefined name 'widget'

If I extend it to State , I get the error:

error: The return type 'SecondRoute' isn't a 'Widget'

I tried to follow the documentation here: https://flutter.dev/docs/cookbook/navigation/navigation-basics

As I mentioned, the navigation itself works, but trying to fetch data from JSON that I had in a widget gives me the error.

Edit:

Text(${place?.text!}") 

The above gives me NULL. Any chance I can get this variable sent to the new page/screen without giving me NULL value?

CodePudding user response:

child: Text(widget.place!.text!)

should be

child: Text(this.place!.text!)

or simply

child: Text(place!.text!)

Although you should think about your usage of !. It should not be neccessary here.

  • Related