Flutter中页面的跳转总结及initialRoute注意事项

基本Route跳转:

跳转:

Navigator.push(

              context,

                  MaterialPageRoute(

                      builder: (context) => second.Parent(),

                      settings: RouteSettings(name: '/Second', arguments: 'This is arguments')));

其中RouteSettings的name属性介绍

  /// The name of the route (e.g., "/settings").

  /// If null, the route is anonymous.

弹出:

Navigator.pop(context);

命名Route跳转:

定义routes信息:

MaterialApp(

      title: 'Flutter Demo',

      theme: ThemeData(

        primarySwatch: Colors.blue,

      ),

      initialRoute: '/',

      routes: {

        '/': (context) => FirstPage(),

        '/second': (context) => SecondPage()

      },

    );

跳转:

Navigator.pushNamed(context, '/Second',arguments: 'This is arguments');

//这里'/Second'是routeName,最终赋值给RouteSettings的name属性

弹出:

Navigator.pop(context);



检索route的顺序:

Generators for routes are searched for in the following order:

1. For the "/" route, the "home" property, if non-null, is used.

2. Otherwise, the "routes" table is used, if it has an entry for the route.

3. Otherwise, onGenerateRoute is called. It should return a non-null value for any valid route not handled by "home" and "routes".

4. Finally if all else fails onUnknownRoute is called.



MaterialApp中initialRoute属性:

官方介绍:

  /// Defaults to [Window.defaultRouteName], which may be overridden by the code

  /// that launched the application.

  ///

  /// If the route contains slashes, then it is treated as a "deep link", and

  /// before this route is pushed, the routes leading to this one are pushed

  /// also. For example, if the route was `/a/b/c`, then the app would start

  /// with the three routes `/a`, `/a/b`, and `/a/b/c` loaded, in that order.

  ///

  /// If any part of this process fails to generate routes, then the

  /// [initialRoute] is ignored and [Navigator.defaultRouteName] is used instead

  /// (`/`). This can happen if the app is started with an intent that specifies

  /// a non-existent route.

  /// The [Navigator] is only built if routes are provided (either via [home],

  /// [routes], [onGenerateRoute], or [onUnknownRoute]); if they are not,

  /// [initialRoute] must be null and [builder] must not be null.

但经实际测试,对于initialRoute:'/a/b/c', 系统也会检测`/`,即

I/flutter (29676): * /

I/flutter (29676):  * /a

I/flutter (29676):  * /a/b

I/flutter (29676):  * /a/b/c

这里我很纳闷,不知道是不是官方文档的问题,欢迎交流讨论。

运行后系统会逐个加载四个route name对应的widget页面,点返回键会依次返回。

一旦one or more of those objects was null, and therefore the initial route specified will be ignored and "/" will be used instead.

错误示例:

1. 如果赋值形如initialRoute:'/a',会报异常但不会crash,会提示'/'和'/a'不能在route表中全被找到就会被默认置为'/':

      initialRoute: '/a',

      routes: {

        '/': (context) => FirstPage(),

        '/b': (context) => SecondPage()

      }

I/flutter (10022): ══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════

I/flutter (10022): The following message was thrown:

I/flutter (10022): Could not navigate to initial route.

I/flutter (10022): The requested route name was: "/a"

I/flutter (10022): The following routes were therefore attempted:

I/flutter (10022):  * /

I/flutter (10022):  * /a

I/flutter (10022): This resulted in the following objects:

I/flutter (10022):  * MaterialPageRoute(RouteSettings("/", null), animation: null)

I/flutter (10022):  * null

I/flutter (10022): One or more of those objects was null, and therefore the initial route specified will be ignored and

I/flutter (10022): "/" will be used instead.


2. 如果'/'不在routes中指定,且没有home属性,就会crash

      initialRoute: '/a',

      routes: {

        '/a': (context) => FirstPage(),

        '/b': (context) => SecondPage()

      }

I/flutter (10022): ══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════

I/flutter (10022): The following message was thrown:

I/flutter (10022): Could not navigate to initial route.

I/flutter (10022): The requested route name was: "/a"

I/flutter (10022): The following routes were therefore attempted:

I/flutter (10022):  * /

I/flutter (10022):  * /a

I/flutter (10022): This resulted in the following objects:

I/flutter (10022):  * null

I/flutter (10022):  * MaterialPageRoute(RouteSettings("/a", null), animation: null)

I/flutter (10022): One or more of those objects was null, and therefore the initial route specified will be ignored and

I/flutter (10022): "/" will be used instead.

I/flutter (10022): ════════════════════════════════════════════════════════════════════════════════════════════════════

I/flutter (10022): Another exception was thrown: Could not find a generator for route RouteSettings("/", null) in the _WidgetsAppState.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3. 注意routes:属性里的'/'不能和home属性同时出现:

      routes: {

        '/': (context) => FirstPage(),

        '/b': (context) => SecondPage()

      }

    home: FirstPage()

I/flutter (10022): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════

I/flutter (10022): The following assertion was thrown building MaterialApp(dirty, state: _MaterialAppState#0574c):

I/flutter (10022): If the home property is specified, the routes table cannot include an entry for "/", since it would

I/flutter (10022): be redundant.

I/flutter (10022): 'package:flutter/src/widgets/app.dart':

I/flutter (10022): Failed assertion: line 169 pos 10: 'home == null ||

I/flutter (10022):          !routes.containsKey(Navigator.defaultRouteName)'

你可能感兴趣的:(Flutter中页面的跳转总结及initialRoute注意事项)