从网络抖动中保存本地图像

时间:2020-05-18 08:30:43

标签: image flutter dart

下面的颤动代码负责从后端node.js恢复图像,我要做的是将图像本地保存在特定路径中,但是当我运行下面的代码时,出现以下错误,我该怎么办?解决这个问题? 我使用cachemanager将图像保存在本地,但是其中一个错误正是在cachemanager上,这是由于什么原因造成的?

错误:

'Image' is imported from both       
'package:pdf/widgets.dart' and 'package:image/src/image.dart'.
import 'package:image/image.dart';                                      
^^^^^                                                                   
lib/Model/Supporto.dart:20:31: Error: The getter 'CacheManager' isn't defined for the
class 'SaveFile'.
 - 'SaveFile' is from 'package:MyApp/Model/Supporto.dart' ('lib/Model/Supporto.dart').
Try correcting the name to the name of an existing getter, or defining a getter or
field named 'CacheManager'.
     var cacheManager = await CacheManager.getInstance()

Dart代码:

    import 'dart:async';
    import 'package:pdf/widgets.dart';
    import 'package:MyApp/Controller/Rapporto.dart';
    import 'package:MyApp/Model/Cantiere.dart';
    import 'package:MyApp/Model/Supporto.dart';
    import 'package:MyApp/Model/Utente.dart' show Utente;
    import 'dart:io' as Io;
    import 'package:image/image.dart';

    import 'package:flutter_cache_manager/flutter_cache_manager.dart';
    import 'package:path_provider/path_provider.dart';

    //Classe che rapprresenta il rapporto
    class Rapporto {
      //Costruttore
      Rapporto(Utente u, Cantiere c) {
        this.utente = u;
        this.c = c;
      }

       //Funzione: permette di recuperare la testata del rapportino
       static Future<bool> getTestata() async{
         bool ret=false;
         String testata=await RapportoController.getTestata(); //http://localhost:8989/rapportini/immagine/testata
        if( testata!=null){
          if(testata.length>0){
             SaveFile sf=new SaveFile();
             final file = await sf.getImageFromNetwork(testata);
             //Recupero il path locale del dispositivo
             var path = await sf.localPath;
             Image image = decodeImage(file.readAsBytesSync());
             //Image thumbnail = copyResize(image, 120,120,);

        //Salvataggio immagine
        try{
          new Io.File('assets/temp/testata.jpg').writeAsBytesSync(encodePng(image));
           ret=true;
        }
        catch(ex){
          ret=false;
        }

          }
        } 
        return ret;
      }
    }

import 'dart:async';
import 'dart:io' as Io;
import 'package:image/image.dart';

import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:path_provider/path_provider.dart';

//Classe che si occupa del salvataggio dei file sul filesystem
class SaveFile {

  //Recupero del path locale
  Future<String> get localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }
  //Recupero dell'immagine dalla rete
   Future<Io.File> getImageFromNetwork(String url) async {

     var cacheManager = await CacheManager.getInstance();
     Io.File file = await cacheManager.getFile(url);
     return file;
   }

}

2 个答案:

答案 0 :(得分:1)

从错误中可以看出,您跟随的两个文件包含相同的Image类。

import 'package:image/image.dart';
import 'package:pdf/widgets.dart';

1)最简单的解决方法是,如果您不需要此文件中的任何一个,则将其删除即可。

2)如果此文件中同时需要两个文件,则必须使用同盟导入文件,以便您可以指定要从while文件中创建哪个对象。

import 'package:image/image.dart' as i;
import 'package:pdf/widgets.dart' as p;

现在,当您要从图像库创建对象时,请按照与pdf文件相同的方式创建

 i.classname variablename = i.classname();

答案 1 :(得分:1)

“图片”是从这两者导入的
'package:pdf / widgets.dart'和'package:image / src / image.dart'。 导入'package:image / image.dart';

看起来这两个导入的软件包都有一个名为Image的实例,请尝试使用hide来忽略不需要的实例,如下所示:

import 'package:pdf/widgets.dart' hide Image;