我的目标是制作一个具有多个WebView
的{{1}}应用程序,当单击每个CupertinoTabViews
时,BottomNavigationBarItem
显示包含不同CupertinoTabScaffold
的小部件; WebView
和pageA
。
在应用程序启动时,第一个WebView成功显示初始URL网页。 但是,当单击另一个“选项卡”按钮(试图显示另一个WebView)时,出现未处理的异常:错误状态:将来已经完成。
main.dart
pageB
pageweb.dart(PageWeb类)
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;
}
}
未处理的异常:错误状态:将来已经完成错误位于以下行:
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;
});
},
))));
}
}
答案 0 :(得分:0)
解决方案很简单。将_controller
声明移到类内。