我正在尝试重建FutureBuilder,在我的情况下,这是为了更改显示给用户的摄像机。为此,我必须运行Future,FutureBuilder再次使用。 未来目前看起来像这样:
body: FutureBuilder(
future: _initializeCameraControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return CameraPreview(_cameraController);
} else {
return Center(child: CircularProgressIndicator());
}
},
),
它正在运行的未来就是这个:
Future<void> _initializeCameraControllerFuture;
@override
void initState() {
super.initState();
if (camerafrontback != true) {
_cameraController =
CameraController(widget.camera[0], ResolutionPreset.veryHigh);
}else{
_cameraController =
CameraController(widget.camera[1], ResolutionPreset.veryHigh);
}
_initializeCameraControllerFuture = _cameraController.initialize();
}
我有一个按钮,该按钮应触发FutureBuilder的此重建,它还更改了“ camerafrontback”布尔值,因此使用了其他摄像机。如下所示:
IconButton(
onPressed: () async {
if (camerafrontback == true){
setState(() {
camerafrontback = false;
});
}else{
setState(() {
camerafrontback = true;
});
};
},
最后,在最后一个括号之前,必须添加一条语句来触发整个FutureBuilder的重建,但是我找不到适合我的代码的语句。
提前感谢您的回答!
答案 0 :(得分:1)
您需要重新初始化_initializeCameraControllerFuture
。
您可以这样做
@override
void initState() {
super.initState();
_init();
}
...
_init() {
if (camerafrontback) {
_cameraController =
CameraController(widget.camera[1], ResolutionPreset.veryHigh);
} else {
_cameraController =
CameraController(widget.camera[0], ResolutionPreset.veryHigh);
}
_initializeCameraControllerFuture = _cameraController.initialize();
}
...
IconButton(
onPressed: () async {
if (camerafrontback) {
camerafrontback = false;
setState(_init());
} else {
camerafrontback = true;
setState(_init);
}
},
),
答案 1 :(得分:-1)
要完全重建您的屏幕,您只需用相同的屏幕替换屏幕
Navigator.pushReplacement(context,
MaterialPageRoute(
builder: (BuildContext context) =>
*SamePage*()
)
);