Flutter 使用Navigator.popUntil()報錯Bad state: Future already completed

有一個需求  從A->B->C->D 結束後返回A頁面 ,但是因爲判斷條件的不同B頁面可能會走可能不走,所以如果使用

Navigator.of(context)..pop()..pop(); 

  這種方式不太方便,希望能有個簡單點的方法 ,於是使用popUntil方法返回 

static void popUntil(BuildContext context, RoutePredicate predicate) {
    Navigator.of(context).popUntil(predicate);
  }

我在跳轉的時候本來使用的是:

 Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) {
      return mPageWidget;
    }));

發現在使用popUtil方法時第二個參數是個RoutePredicate predicate,於是去註冊了一個靜態路由

        routes: <String, WidgetBuilder>{
          '/pageA': (BuildContext context) => new MyPage(),
        },

然後在要返回的地方使用

    Navigator.popUntil(context, ModalRoute.withName('/pageA'));

結果報錯:Bad state: Future already completed

 

解決方法:

不多說 直接上代碼:在跳轉A頁面時使用

    Navigator.of(context).push(
      MaterialPageRoute(
        settings: RouteSettings(name:"/pageA"),
        builder: (context) => MyPage(),
      ),
    );

在返回時使用

    Navigator.popUntil(context, ModalRoute.withName('/pageA'));

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章