我想在setState中使用FutureBuilder,但一直在获取

setState() or markNeedsBuild() called during build.

我已经宣布比率为全局变量。我想更新比率的值。只有成功获得图像的宽度和高度,才能计算比例。
 Widget _showItem() {
    return FutureBuilder(
        future: _bloc.selectImage(),
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.waiting:
              return Center(
                child: CircularProgressIndicator(),
              );
              break;

            case ConnectionState.done:
              if (snapshot.hasData) {
                Image image = new Image.network(snapshot.data);
                Completer<ui.Image> completer = new Completer<ui.Image>();
                image.image.resolve(new ImageConfiguration()).addListener(
                    new ImageStreamListener((ImageInfo image, bool _) {
                  completer.complete(image.image);

                  setState(() {
                num maxHeightRatio = 300 / image.image.height;
                num maxWidthRatio = 400 / image.image.width;
                    this.ratio = maxWidthRatio.coerceAtMost(maxHeightRatio);
                  });
                }));

                return CachedNetworkImage(
                  imageUrl: snapshot.data.toString(),
                  imageBuilder: (context, imageProvider) => Container(
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: imageProvider
                      ),
                    ),
                  ),
                  placeholder: (context, url) => CircularProgressIndicator(),
                  errorWidget: (context, url, error) => Icon(Icons.error),
                );
              } else {
                return Image.asset('assets/no_image.png');
              }
              break;

            default:
              return Text("Error");
          }
        });
  }

我知道我们不应该在setState中使用FutureBuilder,但是由于需要更新全局变量,解决方案是什么?

最佳答案

最好将将来的调用移到initState方法,因为您不想在每次构建小部件时都调用selectImage。一旦将调用移出构建方法之外,就可以进行所有计算,然后进行setState

void initState() {
  super.initState();
  setup();
}

setup() async {
  var data = await _bloc.selectImage();
  // do calculations using data
  // setState
}

阅读更多关于此here的信息。

关于flutter - setState在futureBuilder中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62307826/

10-13 05:26