简体   繁体   English

Flutter:从 newtwork 保存和加载图像

[英]Flutter: Saving and loading Images from the newtwork

We try to save, load and display images from the network, when the user is offline当用户离线时,我们尝试保存、加载和显示来自网络的图像

  void saveImage(String imageUrl, String imageName) async{
    File file = File(await getImagePath(mediaName));
    ByteData questionMediaData = await NetworkAssetBundle(Uri.parse(imageUrl)).load("");
    file.writeAsString(questionMediaData.buffer.asUint8List());
  }

  Future<Uint8List> loadImage(String mediaName) async{
    String path = imageUrl + mediaName;
    if(await File(path).exists()){
      File file = File(await getImagePath(path));
      return await file.readAsString();
    }
    return null;
  }

  Future<String> getImagePath(String imageName) async {
    Directory appDocumentsDirectory = await getApplicationDocumentsDirectory(); 
    String appDocumentsPath = appDocumentsDirectory.path;
    String filePath = '$appDocumentsPath/$imageName';
    return filePath;
  }

We have an ImageProvider-Class:我们有一个 ImageProvider 类:

    class OurImageProvider {

  static Widget getImage(String imageName, {BoxFit fit = BoxFit.cover, double width, double height, ImageType imageType = ImageType.standard}) {

    if(!hasText(imageName)){
      if(imageType == ImageType.content)
        return Image(
            image: AssetImage('assets/dummy_images/catalog_dummy.jpg'),
            fit: BoxFit.cover,
            width: width,
            height: height,
        );
      else
        return Container();
    }


    return Image.network(imageName, fit: fit, width: width, height: height,
      errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) {
        return getAltImage(imageName, width, height, fit);
      },

    );
  }

  static Image getDummyImage(double width, double height, BoxFit fit) => Image(image: AssetImage('assets/dummy_images/dummy.jpg'), width: width, height: height, fit: fit);

  static getAltImage(String imageName, double width, double height, BoxFit fit) async{
      var localImage = await LocalStorage.instance.loadImage(imageName);
      if(localImage != null){
        return Image.memory(localImage, fit: BoxFit.cover,width: width, height: height, errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) {
          return getDummyImage(width, height, fit);
        },);
      }
      return getDummyImage(width, height, fit);
    }
}

Our Problem is now that I don't find a way to fix this class as getAltImage has to be async and I cannot get getImage working to be usable as a child in the build method...我们的问题现在是我找不到修复这个类的方法,因为getAltImage必须是异步的,而且我无法让getImage工作以在构建方法中作为子项使用...

尝试添加await

return Image.memory(await localImage,...);

FutureBuilder was the solution in my case ... FutureBuilder 是我的解决方案......

    return Image.network(url, fit: fit, width: width, height: height,
  errorBuilder: (BuildContext context, Object exception, StackTrace stackTrace) {
    return FutureBuilder(
      future: getAltImage(imageName, width, height, fit),
      builder: (context, snapshot){
        if(snapshot.hasData){
          print(snapshot.data.toString());
          return snapshot.data;
        }else{
          return Container();
        }
      }
    );
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM