【问题标题】:How to connected String to futur of Futurbuilder with flutter?如何使用颤振将字符串连接到 Future Builder 的未来?
【发布时间】:2022-11-12 20:01:44
【问题描述】:

在我的 initStat 从 firebase 加载数据之后,我尝试使用 FuturBuilder 构建一个小部件。

在 init 我调用一个函数来接收来自 firebase 的 url

string url;

   
load_url() async {
url=  await FirebaseStorage.instance.refFromURL('gs://XXXXX-XXXXX.appspot.com/XXXX.jpg').getDownloadURL();
 }

之后我尝试构建一个小部件来显示 url

CachedNetworkImage(
placeholder: (context, url) => Center(child:CircularProgressIndicator(),) ,
imageUrl: '$url',
);

我不能直接使用 CachedNetworkImage,因为 url 是在初始化构建后加载的。所以我在收到 URL 后尝试构建 UI

我尝试像这样使用 FuturBuilder

FutureBuilder(
        future: "$url",
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none: return new Text('');
            case ConnectionState.waiting: return new Text('');
            default:
              if (snapshot.hasError)
                return new Text('Error: ${snapshot.error}');
              else
                return CachedNetworkImage(
                  placeholder: (context, url) => Center(child:CircularProgressIndicator(),) ,
                  imageUrl: '$url',
                );
          }
        },
      );

但我不能写future: "$url", 我有这个错误

The argument type 'String' can't be assigned to the parameter type 'Future<dynamic>?'.

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    将您的 FutureBuilder 更改为:

    FutureBuilder<String>(
        future: FirebaseStorage.instance.refFromURL('gs://XXXXX-XXXXX.appspot.com/XXXX.jpg').getDownloadURL(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none: return new Text('');
            case ConnectionState.waiting: return new Text('');
            default:
              if (snapshot.hasError)
                return new Text('Error: ${snapshot.error}');
              else
                String imageUrl = snapshot.data ?? "";
                return CachedNetworkImage(
                  placeholder: (context, url) => Center(child:CircularProgressIndicator(),) ,
                  imageUrl: imageUrl,
                );
          }
        },
      );
    

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 2022-08-19
      • 2021-03-07
      • 2021-02-23
      • 1970-01-01
      • 2021-04-18
      • 2021-07-02
      • 1970-01-01
      • 2020-03-05
      相关资源
      最近更新 更多