Flutter中頁面的跳轉(zhuǎn)總結(jié)及initialRoute注意事項(xiàng)

基本Route跳轉(zhuǎn):

跳轉(zhuǎn):

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跳轉(zhuǎn):

定義routes信息:

MaterialApp(

? ? ? title: 'Flutter Demo',

? ? ? theme: ThemeData(

? ? ? ? primarySwatch: Colors.blue,

? ? ? ),

? ? ? initialRoute: '/',

? ? ? routes: {

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

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

? ? ? },

? ? );

跳轉(zhuǎn):

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.

但經(jīng)實(shí)際測試割去,對于initialRoute:'/a/b/c', 系統(tǒng)也會(huì)檢測`/`,即

I/flutter (29676): * /

I/flutter (29676):? * /a

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

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

這里我很納悶醉拓,不知道是不是官方文檔的問題,歡迎交流討論套鹅。

運(yùn)行后系統(tǒng)會(huì)逐個(gè)加載四個(gè)route name對應(yīng)的widget頁面轧拄,點(diǎn)返回鍵會(huì)依次返回挥下。

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

錯(cuò)誤示例:

1. 如果賦值形如initialRoute:'/a'揍魂,會(huì)報(bào)異常但不會(huì)crash,會(huì)提示'/'和'/a'不能在route表中全被找到就會(huì)被默認(rèn)置為'/':

? ? ? 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屬性现斋,就會(huì)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屬性同時(shí)出現(xiàn):

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市偎蘸,隨后出現(xiàn)的幾起案子庄蹋,更是在濱河造成了極大的恐慌瞬内,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,816評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件限书,死亡現(xiàn)場離奇詭異虫蝶,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)倦西,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,729評論 3 385
  • 文/潘曉璐 我一進(jìn)店門能真,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人扰柠,你說我怎么就攤上這事粉铐。” “怎么了耻矮?”我有些...
    開封第一講書人閱讀 158,300評論 0 348
  • 文/不壞的土叔 我叫張陵秦躯,是天一觀的道長。 經(jīng)常有香客問我裆装,道長,這世上最難降的妖魔是什么倡缠? 我笑而不...
    開封第一講書人閱讀 56,780評論 1 285
  • 正文 為了忘掉前任哨免,我火速辦了婚禮,結(jié)果婚禮上昙沦,老公的妹妹穿的比我還像新娘琢唾。我一直安慰自己,他們只是感情好盾饮,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,890評論 6 385
  • 文/花漫 我一把揭開白布采桃。 她就那樣靜靜地躺著,像睡著了一般丘损。 火紅的嫁衣襯著肌膚如雪普办。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,084評論 1 291
  • 那天徘钥,我揣著相機(jī)與錄音衔蹲,去河邊找鬼。 笑死呈础,一個(gè)胖子當(dāng)著我的面吹牛舆驶,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播而钞,決...
    沈念sama閱讀 39,151評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼沙廉,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了臼节?” 一聲冷哼從身側(cè)響起撬陵,我...
    開封第一講書人閱讀 37,912評論 0 268
  • 序言:老撾萬榮一對情侶失蹤珊皿,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后袱结,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體亮隙,經(jīng)...
    沈念sama閱讀 44,355評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,666評論 2 327
  • 正文 我和宋清朗相戀三年垢夹,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了溢吻。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,809評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡果元,死狀恐怖促王,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情而晒,我是刑警寧澤蝇狼,帶...
    沈念sama閱讀 34,504評論 4 334
  • 正文 年R本政府宣布,位于F島的核電站倡怎,受9級特大地震影響迅耘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜监署,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,150評論 3 317
  • 文/蒙蒙 一颤专、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧钠乏,春花似錦栖秕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至俏拱,卻和暖如春暑塑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背彰触。 一陣腳步聲響...
    開封第一講書人閱讀 32,121評論 1 267
  • 我被黑心中介騙來泰國打工梯投, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人况毅。 一個(gè)月前我還...
    沈念sama閱讀 46,628評論 2 362
  • 正文 我出身青樓分蓖,卻偏偏與公主長得像,于是被迫代替她去往敵國和親尔许。 傳聞我的和親對象是個(gè)殘疾皇子么鹤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,724評論 2 351

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,312評論 0 10
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,448評論 0 13
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,028評論 0 2
  • 誰的尿尿在了馬桶沿上 像個(gè)頑皮的孩子,盤在樹上 墻壁是披著白發(fā)的天空 手紙里開出燦爛的小黃花 有人在肚子里架起爐灶...
    樹下老狗閱讀 207評論 0 3
  • 這一章節(jié)是群體領(lǐng)袖及其說服方式的上篇味廊,重點(diǎn)講領(lǐng)袖和領(lǐng)袖的作用方式蒸甜,“領(lǐng)袖起初往往都是被領(lǐng)導(dǎo)者棠耕,他本人也需要被某種思...
    妙丫丫POI閱讀 150評論 0 0