【发布时间】:2021-04-21 15:56:39
【问题描述】:
我有以下 FutureBuilder 条目。
FutureBuilder(
future: _checkConn,
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState){
case ConnectionState.none:
Navigator.pushReplacementNamed(context, NoConnView);
break;
case ConnectionState.active:
case ConnectionState.waiting:
case ConnectionState.done:
if(snapshot.data=='OK'){
initialfbget();
break;
} else {
Navigator.pushReplacementNamed(context, NoConnView);
break;
}
}
return SizedBox.expand(
child: FittedBox(
fit: BoxFit.fill,
child: SizedBox(
width: _vcontroller.value.size?.width ?? (MediaQuery.of(context).size.width),
height: _vcontroller.value.size?.height ?? (MediaQuery.of(context).size.height),
child: VideoPlayer(_vcontroller),
),
),
);
}
),
以下是完整的 initstate 部分:
void initState() {
super.initState ();
_vcontroller = VideoPlayerController.asset("assets/testimages/sukiitestanimation.mp4")
..initialize().then((_) {
// Once the video has been loaded we play the video and set looping to true.
_vcontroller.play();
_vcontroller.setLooping(false);
// Ensure the first frame is shown after the video is initialized.
});
_checkConn = checkConn();
Firebase.initializeApp();
}
下面是checkconn段:
Future<String> checkConn() async {
var connresponse = await http.get(connurl).timeout(const Duration(seconds: 10));
log('connresponse is: ${connresponse.statusCode}');
if(connresponse.statusCode!=200) {
return "BAD";
} else {
return "OK";
}
}
不断收到以下错误。
在构建期间调用 setState() 或 markNeedsBuild()。
在这方面的任何帮助都将不胜感激。
提前致谢。
【问题讨论】:
-
您能分享您的
checkConn()和完整的initState()方法吗?它将帮助另一个志愿者回答这个问题 -
```Future
checkConn() async { var connresponse = await http.get(connurl).timeout(const Duration(seconds: 10)); log('connresponse 是:${connresponse.statusCode}'); if(connresponse.statusCode!=200) { return "BAD"; } 其他 { 返回 "OK"; } } -
@GilangPratama,重新粘贴请求的信息。
Future<String> checkConn() async { var connresponse = await http.get(connurl).timeout(const Duration(seconds: 10)); log('connresponse is: ${connresponse.statusCode}'); if(connresponse.statusCode!=200) { return "BAD"; } else { return "OK"; } }. -
void initState() { super.initState (); _vcontroller = VideoPlayerController.asset("assets/testimages/sukiitestanimation.mp4") ..initialize().then((_) { // Once the video has been loaded we play the video and set looping to true. _vcontroller.play(); _vcontroller.setLooping(false); // Ensure the first frame is shown after the video is initialized. }); _checkConn = checkConn(); Firebase.initializeApp(); }
标签: flutter flutter-futurebuilder