Why is the value null of a variable on my next screen in Flutter?
The text value widget.place.texty works perfectly in my widget. I am parsing JSON and fetching the info.
However, if I try to pass this value to the next screen, I am suddenly getting a NULL
or a full empty area.
This is the secondPage I am trying to create:
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);
},
Text("${place?.texty ??""}"),
),
),
);
}
And:
class PlaceDetail extends StatefulWidget {
final Place? place;
PlaceDetail({Key? key, this.place}) : super(key: key);
@override
_PlaceDetailState createState() {
return _PlaceDetailState();
}
}
I tried to follow the documentation here: https://flutter.dev/docs/cookbook/navigation/navigation-basics
The navigation itself works, but trying to show data from screen 1 on screen 2 with the same variable, does not seem to work.
And this is how I have build the button (on the first page) to click to the second page.
Center(
child: ElevatedButton(
child: const Text('Open route'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondRoute()),
);
},
),
),
CodePudding user response:
You need to pass the place value to second route.
Center(
child: ElevatedButton(
child: const Text('Open route'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const SecondRoute(place: widget.place)),
);
},
),
),