Home > OS >  how to pass multiple data to another screen flutter
how to pass multiple data to another screen flutter

Time:10-31

I already have a method to pass data but it can only pass one arguments but I want it to pass at least 2 arguments how can I do that.

code:

screen1

    GestureDetector(
        onTap: () => Navigator.of(context).push(PageTransition(settings: RouteSettings(
       arguments:imageUrl,),type: PageTransitionType.fade,
       child: const ShowPictureScreen())),

Receive on Screen2

@override
  Widget build(BuildContext context) {
    final data = ModalRoute.of(context)!.settings;
    late String photoUrl;
    if (data.arguments == null) {
      photoUrl = "empty";
    } else {
      photoUrl = data.arguments as String;
    }

...

Text('photoUrl')

CodePudding user response:

you can pass multiple data using the following 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..
          ],
        ),
      )
    )
}

CodePudding user response:

You can pass argements throgh Map

arguments: {
  "imageUrl": imageUrl,
  "name": name,
  "email": email
}

and you can receive like this

final data = ModalRoute.of(context)!.settings.arguments as Map;
String photoUrl = data["imageUrl"];
  • Related