Reputation: 1275
I'm new to Flutter. I'm trying to send multiple data to another screen:
// screen1.dart
..
Expanded(
child: RaisedButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(
builder: (context) => new Screen2(name: thing.name, email: thing.email, address: thing.address, etc..),
),
);
},
),
),
..
// screen2.dart
class Screen2 extends StatefulWidget{
Screen2({this.name}, {this.email}, {this.address}, etc..);
final String name;
final String email;
final String address;
// etc
@override
State<StatefulWidget> createState() { return new Screen2State();}
}
class Screen2State extends State<Screen2> {
Widget build(BuildContext context) {
return new WillPopScope(
..
child: Scaffold(
..
new Row(
children: <Widget>[
new Text(widget.name),
new Text(widget.email),
new Text(widget.address),
etc..
],
),
)
)
}
But I get the error: A non-null String must be provided to a Text widget.
The data is transferred from TextEditingControllers. It works when there is only 1 data transferred, but fails when there are 2 or more.
What's the correct way to send multiple data between screens?
Upvotes: 5
Views: 11686
Reputation: 1391
Everything looks fine but you need to change in the Screen 2 class constructor to this
Screen2({this.name, this.email, this.address, etc..});
Modified Code
// screen1.dart
..
Expanded(
child: RaisedButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(
builder: (context) => new Screen2(name: thing.name, email: thing.email, address: thing.address, etc..),
),
);
},
),
),
..
// screen2.dart
class Screen2 extends StatefulWidget{
Screen2({this.name, this.email, this.address, etc..});
final String name;
final String email;
final String address;
// etc
@override
State<StatefulWidget> createState() { return new Screen2State();}
}
class Screen2State extends State<Screen2> {
Widget build(BuildContext context) {
return new WillPopScope(
..
child: Scaffold(
..
new Row(
children: <Widget>[
new Text(widget.name),
new Text(widget.email),
new Text(widget.address),
etc..
],
),
)
)
}
Note: Text Widget will not accept null values so please make sure you are passing all the values. Or you can initialize the variables with the default value to blank
final String name="";
final String email="";
final String address="";
Upvotes: 3
Reputation: 108
Consider passing the arguments through route arguments. Refer official doc here https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments
Upvotes: 2