简体   繁体   English

Flutter 多个 WebView 小部件错误“错误 state:未来已完成”

[英]Flutter multiple WebView widget error "Bad state: Future already completed"

My goal is to make a WebView app with several CupertinoTabViews that when clicking each BottomNavigationBarItem , CupertinoTabScaffold shows different WebView containing widgets; CupertinoTabViews WebView WebView BottomNavigationBarItem CupertinoTabScaffold pageA and pageB . pageApageB

As the application launches, the first WebView successfully shows the initial URL webpage.随着应用程序启动,第一个 WebView 成功显示初始 URL 网页。 However, when clicking another Tab button (trying to show another WebView), Unhandled Exception: Bad state: Future already completed is thrown.但是,当单击另一个 Tab 按钮(试图显示另一个 WebView)时,抛出未处理的异常:错误 state:未来已完成

main.dart主要.dart

class _MyHomePageState extends State<MyHomePage> {
  int _currentTabIndex = 0;

  PageWeb pageA;
  PageWeb pageB;

  @override
  void initState() {
    super.initState();

    pageA = new PageWeb(this, ObjectKey(0), "A");
    pageB = new PageWeb(this, ObjectKey(1), "B");
  }

  @override
  Widget build(BuildContext context) {
    final Widget scaffold = CupertinoTabScaffold(
      tabBar: CupertinoTabBar(
        currentIndex: _currentTabIndex,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: Icon(Icons.home), title: Text('A')),
          BottomNavigationBarItem(
              icon: Icon(Icons.photo), title: Text('B')),
        
        ],
      ),
      tabBuilder: (context, index) {
        CupertinoTabView returnValue;
        switch (index) {
          case 0:
            returnValue = CupertinoTabView(
              builder: (context) {
                return pageA;
              },
            );
            break;
          case 1:
            returnValue = CupertinoTabView(
              builder: (context) {
                return pageB;
              },
            );
            break;
        }
        return returnValue;
      },
    );

    return scaffold;
  }
}

pageweb.dart (PageWeb class) pageweb.dart(PageWeb 类)

final Completer<WebViewController> _controller = Completer<WebViewController>();

class PageWeb extends StatefulWidget {
  final delegate;
  final ObjectKey key;
  final String navTitle;

  PageWeb(this.delegate, this.key, this.navTitle);

  @override
  _PageWebState createState() {
    return _PageWebState();
  }
}

class _PageWebState extends State<PageWeb> {
  bool _isLoading = true;
  final cookieManager = WebviewCookieManager();

  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
        navigationBar: CupertinoNavigationBar(
          middle: Text(widget.navTitle),
        ),
        child: SafeArea(
            child: Container(
                child: new WebView(
          key: widget.key,
          initialUrl: "https://www.google.com/search?q=${widget.navTitle}",
          javascriptMode: JavascriptMode.unrestricted,
          onWebViewCreated: (WebViewController webViewController) {
            _controller.complete(webViewController);
            // Here is the error occurring part.
          },
          onPageStarted: (String pageUrl) {
            setState(() {
              _isLoading = true;
            });
          },
          onPageFinished: (String pageUrl) {
            setState(() {
              _isLoading = false;
            });
          },
        ))));
  }
}

The Unhandled Exception: Bad state: Future already completed error locates the following line: Unhandled Exception: Bad state: Future already completed错误位于以下行:

_controller.complete(webViewController);

The solution was simple.解决方案很简单。 Move the _controller declaration inside the class._controller声明移到 class 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 安装错误,“错误状态:未来已经完成” - Flutter install error, "Bad state: Future already completed" Flutter Dio 拦截器错误:错误 state:未来已完成 - Flutter Dio interceptor Error: Bad state: Future already completed 坏 state:未来已经在 Flutter 完成 - Bad state: Future already completed in Flutter 未处理的异常:状态不佳:未来已在颤动中完成 - Unhandled Exception: Bad state: Future already completed in flutter Flutter:Google Maps StateError(错误状态:未来已经完成) - Flutter: Google Maps StateError (Bad state: Future already completed) Mac OS Big sur Flutter 安装错误,“状态不佳:未来已经完成” - Mac OS Big sur Flutter install error, “Bad state: Future already completed” google_maps_flutter 插件不适用于 Android 显示错误错误 state:未来已完成 - google_maps_flutter plugin not working on Android shows Error Bad state: Future already completed 未处理的异常:错误状态:未来已经完成 - Unhandled exception: Bad state: Future already completed 例外:坏 state:未来已经完成 - Exception: bad state: Future already completed 在Android工作室中运行和热重载颤动的问题(糟糕的状态:未来已经完成) - Trouble with running and hot reloading flutter in Android studio(Bad state: Future already completed)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM