【发布时间】: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>?'.
【问题讨论】: