Flutter Passing Data to Stateful Widget

If you want to pass data between two pages or another page (Stateful Widget), Flutter provides routes mechanism. To implement, You have to define routes in MaterialApp, Just like this :

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        title: 'Pass Data Example',
        theme: ThemeData(
          primarySwatch: Colors.teal, //indigo, purple, teal
        ),
        home: SplashPage(),
        routes: {
          '/home': (context) => HomePage(), // launch page without passing data
        },
        onGenerateRoute: (RouteSettings settings) {
          var routes = <String, WidgetBuilder>{
            '/second_page': (context) => Secondpage(data: settings.arguments),
          };
          WidgetBuilder builder = routes[settings.name];
          return MaterialPageRoute(builder: (ctx) => builder(ctx));
        });
  }
}

After defining this route in Material App then you have to pass data as a argument like this :

Navigator.of(context).pushNamed("/second_page", arguments: passingData);

Related Questions :

  • Pass data to other page in Flutter
  • Pass data between two page in Flutter
Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *