Flutter 多个 WebView 小部件错误 "Bad state: Future already completed"
Flutter multiple WebView widget error "Bad state: Future already completed"
我的目标是制作一个包含多个 CupertinoTabViews
的 WebView
应用程序,单击每个 BottomNavigationBarItem
时,CupertinoTabScaffold
会显示不同的 WebView
小部件; pageA
和 pageB
.
应用程序启动时,第一个 WebView 成功显示初始 URL 网页。
但是,当单击另一个 Tab 按钮(试图显示另一个 WebView)时,抛出 未处理的异常:错误状态:未来已完成。
main.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)
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 错误位于以下行:
_controller.complete(webViewController);
解决方法很简单。将 _controller
声明移到 class.
中
我的目标是制作一个包含多个 CupertinoTabViews
的 WebView
应用程序,单击每个 BottomNavigationBarItem
时,CupertinoTabScaffold
会显示不同的 WebView
小部件; pageA
和 pageB
.
应用程序启动时,第一个 WebView 成功显示初始 URL 网页。 但是,当单击另一个 Tab 按钮(试图显示另一个 WebView)时,抛出 未处理的异常:错误状态:未来已完成。
main.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)
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 错误位于以下行:
_controller.complete(webViewController);
解决方法很简单。将 _controller
声明移到 class.