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)'

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,142评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,298评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,068评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,081评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,099评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,071评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,990评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,832评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,274评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,488评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,649评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,378评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,979评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,625评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,643评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,545评论 2 352

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,322评论 0 10
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,451评论 0 13
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,029评论 0 2
  • 谁的尿尿在了马桶沿上 像个顽皮的孩子,盘在树上 墙壁是披着白发的天空 手纸里开出灿烂的小黄花 有人在肚子里架起炉灶...
    树下老狗阅读 213评论 0 3
  • 这一章节是群体领袖及其说服方式的上篇,重点讲领袖和领袖的作用方式,“领袖起初往往都是被领导者,他本人也需要被某种思...
    妙丫丫POI阅读 151评论 0 0