How to pass parameter runApp(MyApp());

Viewed 1307

I have code as below, it's no problems.

void main() async {
  Widget _defaultHome = new LoginPage();

  runApp(new MaterialApp(
    title: 'App',
    debugShowCheckedModeBanner: false,
    home: _defaultHome,
    routes: <String, WidgetBuilder>{
      // Set routes for using the Navigator.
      '/tabs': (BuildContext context) => new TabsPage(),
      '/login': (BuildContext context) => new LoginPage()
    },
  ));
}

I want to change it to code as below, how can I pass _defaultHome to class MyApp?

void main() async {
  Widget _defaultHome = new LoginPage();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    // final textTheme = Theme.of(context).textTheme;
    return MaterialApp(
      title: 'Gaia',
      debugShowCheckedModeBanner: false,
      home: _defaultHome,
      routes: <String, WidgetBuilder>{
        // Set routes for using the Navigator.
        '/tabs': (BuildContext context) => new TabsPage(),
        '/login': (BuildContext context) => new LoginPage()
      },
    );
  }
}
3 Answers

You can either decide to pass it via Provider or via parameters.

This is the way you pass it via parameters with optional parameters:

void main() async {
  final Widget _defaultHome = new LoginPage();
  runApp(MyApp(homePage: _defaultHome));
}

class MyApp extends StatelessWidget {

  MyApp({this.homePage});

  final Widget homePage;

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    // final textTheme = Theme.of(context).textTheme;
    return MaterialApp(
      title: 'Gaia',
      debugShowCheckedModeBanner: false,
      home: _defaultHome,
      routes: <String, WidgetBuilder>{
        // Set routes for using the Navigator.
        '/tabs': (BuildContext context) => new TabsPage(),
        '/login': (BuildContext context) => homePage
      },
    );
  }
}

You can pass it by a parameters in your constructor and assign it to your home.

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  final Widget defaultHome;

  const MyApp({Key key, @required this.defaultHome}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // final textTheme = Theme.of(context).textTheme;
    return MaterialApp(
      title: 'Gaia',
      debugShowCheckedModeBanner: false,
      home: defaultHome,
      routes: <String, WidgetBuilder>{
        // Set routes for using the Navigator.
        '/tabs': (BuildContext context) => new TabsPage(),
        '/login': (BuildContext context) => new LoginPage()
      },
    );
  }
}

Use the below code and any parameter can pass through constructor.

  void main() async {
      Widget _defaultHome = new LoginPage();
      runApp(MyApp(defaultHome: _defaultHome,));
    }
    
    class MyApp extends StatelessWidget {
     final Widget defaultHome;
      
        const MyApp({
        @required this.defaultHome,
        Key key,
      }) : super(key: key);
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        // final textTheme = Theme.of(context).textTheme;
        return MaterialApp(
          title: 'Gaia',
          debugShowCheckedModeBanner: false,
          home: defaultHome,
          routes: <String, WidgetBuilder>{
            // Set routes for using the Navigator.
            '/tabs': (BuildContext context) => new TabsPage(),
            '/login': (BuildContext context) => new LoginPage()
          },
        );
      }
    }
Related