首页 文章

将来与Flutter中的FutureBuilder一起返回null

提问于
浏览
0

我想使用FutureBuilder来检查url是否是png图像,然后构建一个或两个图像(在列表中) . 但不知何故,当我打印它时,Future总是返回null ...

结果是应用程序总是使用两个CachedNetworkImages构建listview,这不是我想要它做的 . 如果URL是图像,它应该只使用该URL构建CachedNetworkImage,如果不是,则应该更改URL并构建包含2个图像的listview .

child: new FutureBuilder(
                future: _getImages(widget.imgUrl),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  switch (snapshot.connectionState) {
                    case ConnectionState.none:
                      return new Text('Press button to start');
                    case ConnectionState.waiting:
                      return new Text('Awaiting result...');
                    default:
                      if (snapshot.hasError)
                        return new Text('Error: ${snapshot.error}');
                      else {
                        print(snapshot.data);
                        if (snapshot.data == "image/png") {
                          return new SingleChildScrollView(
                            child: new CachedNetworkImage(
                              imageUrl: widget.imgUrl,
                              placeholder: new Center(
                                  child: new AdaptiveProgressIndicator()),
                            ),
                          );
                        } else {
                          return new ListView(
                            children: <Widget>[
                              new CachedNetworkImage(
                                imageUrl:
                                    widget.imgUrl.split('.png')[0] + '-0.png',
                                placeholder: new Center(
                                    child: new AdaptiveProgressIndicator()),
                              ),
                              new CachedNetworkImage(
                                imageUrl:
                                    widget.imgUrl.split('.png')[0] + '-1.png',
                              )
                            ],
                          );
                        }
                      }
                  }
                }),
          ),
        ));
  }

  Future<String> _getImages(String url) async {
    await http.get(url).then((result) {
      return result.headers['content-type'];
    });
  }

2 回答

  • 1

    这段代码有点奇怪 .

    Future<String> _getImages(String url) async {
      await http.get(url).then((result) {
        return result.headers['content-type'];
      });
    }
    

    async 允许您避免 then (在大多数情况下) .

    代码应该是:

    Future<String> _getImages(String url) async {
      var result = await http.get(url);
      // for debugging only
      print('statusCode: ${result.statusCode}');
      var contentType = result.headers['content-type'];
      print('content-type: $contentType');
      return contentType;
    }
    

    这样,您还可以检查请求是否实际提供了结果 .

  • 0

    你的 _getImages 函数什么都不返回 . 您需要返回通话结果 .

    Future<String> _getImages(String url) async {
       return await http.get(url).then((result) {
          return result.headers['content-type'];
       });
    }
    

相关问题