簡體   English   中英

未處理的異常:狀態不佳:未來已在顫動中完成

[英]Unhandled Exception: Bad state: Future already completed in flutter

你好,當我導航到我的應用程序中的另一個頁面時出現此錯誤

我不知道為什么會出現這個錯誤

#0      _AsyncCompleter.complete  (dart:async/future_impl.dart:39:31)
#1      Route.didComplete 
package:flutter/…/widgets/navigator.dart:203
#2      NavigatorState.pushReplacement.<anonymous closure> 
package:flutter/…/widgets/navigator.dart:1861
#3      TickerFuture.whenCompleteOrCancel.thunk 
package:flutter/…/scheduler/ticker.dart:389
#4      _rootRunUnary  (dart:async/zone.dart:1132:38)
#5      _CustomZone.runUnary  (dart:async/zone.dart:1029:19)
#6      _FutureListener.handleValue  (dart:async/future_impl.dart:137:18)
#7      Future._propagateToListeners.handleValueCallback  (dart:async/future_impl.dart:678:45)
#8      Future._propagateToListeners  (dart:async/future_impl.dart:707:32)
#9      Future._completeWithValue  (dart:async/future_impl.dart:522:5)
#10     Future._asyncComplete.<anonymous closure>  (dart:async/future_impl.dart:552:7)
#11     _rootRun  (dart:async/zone.dart:1124:13)
#12     <…> 

導航代碼是

Navigator.pop(context);
Navigator.pushReplacementNamed(context, '/NavigationBar');

路線是這樣的

 routes: {
        "/": (BuildContext context) => LoginPage(),
        "/NavigationBar": (BuildContext context) => NavigationBarPage(),

      },

我已經用官方示例重現了這個錯誤
刪除Navigator.pop(context); 工作正常

在演示中,第一個屏幕單擊按鈕轉到第二個屏幕。
你不需要Navigator.pop(context);
只是Navigator.pushReplacementNamed(context, '/second'); 將工作
代碼片段

onPressed: () {
            //Navigator.pop(context); remove this line
            // Navigate to the second screen using a named route.
            Navigator.pushReplacementNamed(context, '/second');
          },

完整代碼

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Named Routes Demo',
    // Start the app with the "/" named route. In this case, the app starts
    // on the FirstScreen widget.
    initialRoute: '/',
    routes: {
      // When navigating to the "/" route, build the FirstScreen widget.
      '/': (context) => FirstScreen(),
      // When navigating to the "/second" route, build the SecondScreen widget.
      '/second': (context) => SecondScreen(),
    },
  ));
}

class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Screen'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Launch screen'),
          onPressed: () {
            //Navigator.pop(context); remove this line
            // Navigate to the second screen using a named route.
            Navigator.pushReplacementNamed(context, '/second');
          },
        ),
      ),
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            // Navigate back to the first screen by popping the current route
            // off the stack.
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}

如果有 Navigator.pop(context) 則出現錯誤信息; 在 Navigator.pushReplacementNamed 之前

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM