Home > Mobile >  Flutter how to save List<XFile>
Flutter how to save List<XFile>

Time:10-06

I am trying to save a list that I get from image picker. It contains images or videos. I want to save it locally and open it on another page in my app.

I have started with this code:

List<XFile>? _imageFileList;

final String path = await getApplicationDocumentsDirectory().path;
final File newImage = await _imageFileList.copy('$path/image1.png');

But I think I need to do it in another way. Does anyone know how I can save the list?

CodePudding user response:

Okay, The code you are giving me gives me errors. I have tried to fix your code like this:

  int index = 0;
  String path = '';
  _function(String path, XFile x) async {
    final Directory directory = await getApplicationDocumentsDirectory();
    path = directory.path;
    File c = File(path).createSync(); //File has to be created first
    c.writeAsBytesSync(await x.readAsBytes());
  }

  saveFiles() {
    int index = 0;
    _imageFileList?.forEach((x) {
      _function("$path/image$index.jpg", x);
      index  ;
    });
  }

But it isn't working enter image description here

CodePudding user response:

I think this is what do you want, I recently did it..

final ImagePicker _picker = ImagePicker();

  File? file;
  List<XFile>? photo = <XFile>[];
  List<XFile> itemImagesList = <XFile>[];

  Future pickPhotoFromGallery() async {
    photo = await _picker.pickMultiImage();
    if (photo != null) {
      itemImagesList = itemImagesList   photo!;
      photo!.clear();
    }
  }

  Future saveItemInfo() async {
    PickedFile? pickedFile;
    for (int i = 0; i < itemImagesList.length; i  ) {
      file = File(itemImagesList[i].path);
      pickedFile = PickedFile(file!.path);
    }
  }
  • Related