Flutter Web: Converting dart:io File to dart:html File

1.9k Views Asked by At

Is there a way to convert dart:io File object to dart:html File? I tried html.File file = dartFile as html.File and it isn't working

2

There are 2 best solutions below

0
On BEST ANSWER

No. The two file objects are completely unrelated.

I am not aware of any platform which has both the dart:io and the dart:html library available, so even being able to import both in the same program is surprising.

0
On

No way. But you can handle it by having different code. See bellow:

  final _photos = <File>[];
  final _photosWeb = <html.File>[];

 if (kIsWeb == false) { //if its not web, handle dart.io file
  final pickedFile = await _picker.getImage(
    source: ImageSource.gallery,
    imageQuality:
        100,
  );

  File image = File(pickedFile!.path);
  if (image != null) {

    _photos.insert(_numberOfImage, image);
  }
} else { //if its web, handle html.file

  final temp = (await ImagePicker()
      .getImage(source: ImageSource.camera, imageQuality: 80));
  final pickedFile = await temp!.readAsBytes();

  var image = html.File(temp.path.codeUnits, temp.path);
  if (image != null) {
    
    _photosWeb.insert(_numberOfImage, image);
  }
}