gpt4 book ai didi

flutter - 重建FutureBuilder

转载 作者:行者123 更新时间:2023-12-03 04:48:37 25 4
gpt4 key购买 nike

我正在尝试重建FutureBuilder,在我的情况下,这是为了更改显示给用户的相机。为此,我必须运行Future,FutureBuilder再次使用。
future 目前看起来像这样:

body: FutureBuilder(
future: _initializeCameraControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return CameraPreview(_cameraController);
} else {
return Center(child: CircularProgressIndicator());
}
},
),

它正在运行的 future 就是这个:
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” bool(boolean) 值,因此使用了其他摄像机。如下图所示:
IconButton(
onPressed: () async {
if (camerafrontback == true){
setState(() {
camerafrontback = false;
});
}else{
setState(() {
camerafrontback = true;
});
};

},

最后,在最后一个括号之前,必须添加一条语句来触发整个FutureBuilder的重建,但是我找不到适合我的代码的语句。

预先感谢您的回答!

最佳答案

您需要重新初始化_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);
}
},
),

关于flutter - 重建FutureBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61808043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com