如何cast/convert Future<dynamic> 成Image?
How to cast/convert Future<dynamic> into Image?
我有像
一样获取图像的功能
dynamic imgBinary = _repository.fetchImage(productId);
我想将其添加到图像列表中
List<NetworkImage> listImages = new List<NetworkImage>();
很喜欢
dynamic imgBinary = _repository.fetchImage(productId);
listImages.add(imgBinary);
这个怎么投?
编辑:anmol.majhail 的回答更好
您的 fetchImage 方法需要 return 未来,这里有一些伪代码作为指导
List<NetworkImage> listImages = new List<NetworkImage>();
Future<void> _fetchAddImageToList(int productId) async {
//trycatch
dynamic imgBinary = await _repository.fetchImage(productId);
listImages.add(imgBinary);
}
Future<NetworkImage> fetchImage(int id) async {
New NetworkImage img = new NetworkImage();
//do your fetch work here
return img;
}
好的,你可以试试.then
方法。
因为 _repository.fetchImage(productId);
是未来。
所以你可以试试 -
List<NetworkImage> listImages = List<NetworkImage>();
Future<dynamic> imgBinary = _repository.fetchImage(productId);
imgBinary.then((i){
listImages.add(i);
});
或
直接:
_repository.fetchImage(productId).then((i){
listImages.add(i);});
要从 Future 中获取值 - 我们可以使用:
async and await
或
您可以使用 then()
方法注册回调。当 Future 完成时会触发此回调。
更多info
我有像
一样获取图像的功能dynamic imgBinary = _repository.fetchImage(productId);
我想将其添加到图像列表中
List<NetworkImage> listImages = new List<NetworkImage>();
很喜欢
dynamic imgBinary = _repository.fetchImage(productId);
listImages.add(imgBinary);
这个怎么投?
编辑:anmol.majhail 的回答更好
您的 fetchImage 方法需要 return 未来,这里有一些伪代码作为指导
List<NetworkImage> listImages = new List<NetworkImage>();
Future<void> _fetchAddImageToList(int productId) async {
//trycatch
dynamic imgBinary = await _repository.fetchImage(productId);
listImages.add(imgBinary);
}
Future<NetworkImage> fetchImage(int id) async {
New NetworkImage img = new NetworkImage();
//do your fetch work here
return img;
}
好的,你可以试试.then
方法。
因为 _repository.fetchImage(productId);
是未来。
所以你可以试试 -
List<NetworkImage> listImages = List<NetworkImage>();
Future<dynamic> imgBinary = _repository.fetchImage(productId);
imgBinary.then((i){
listImages.add(i);
});
或
直接:
_repository.fetchImage(productId).then((i){
listImages.add(i);});
要从 Future 中获取值 - 我们可以使用:
async and await
或
您可以使用 then()
方法注册回调。当 Future 完成时会触发此回调。
更多info