Want to pass non-string data to a named route in Flutter? You can use the parameter routes of your App for directly passing arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', initialRoute: HomePage.route, routes: { HomePage.route: (_) => HomePage(), DetailsPage.route: (context) => DetailsPage(ModalRoute.of(context).settings.arguments), }, ); } } class HomePage extends StatelessWidget { static const String route = '/'; @override Widget build(BuildContext context) { return Scaffold( body: Container(), floatingActionButton: FloatingActionButton( onPressed: () { Navigator.pushNamed(context, '/details', arguments: ScreenArguments( 'My Details', 'Some Message', )); }, ), ); } } class DetailsPage extends StatelessWidget { static const String route = '/details'; final ScreenArguments arguments; DetailsPage(this.arguments); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(arguments.title), ), body: Center( child: Text(arguments.message), ), ); } } class ScreenArguments { final String title; final String message; ScreenArguments(this.title, this.message); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.