'How To Compress Image Size To 2 MB In Flutter Web

I Am Trying To Create An App That Will Capture Image In Both Android And Web in Android With The Help Of Image_Picker Is Fine But In Web Don't Know How Do I Compress image.

   final result = await FilePicker.platform.pickFiles(
    allowMultiple: false,
    type: FileType.custom,
    allowedExtensions: ["png", "jpg"],
  ).catchError((error) {
    ScaffoldMessenger.of(context)
        .showSnackBar(SnackBar(content: Text("Error: $error")));
  });
  if (result == null) {
    return;
  }
  final filepath = result.files.single.bytes;
  final filename = result.files.single.name;

  setState(() {
    img1 = (filepath!);
    nameoffile = io.File(filename);
  });
  // // inserIntoFireStoreWebGallery(email: email); // print(nameoffile);
  var convertToKiloByte = img1!.lengthInBytes / 1024;
  var covertToMegaByte = convertToKiloByte / 1024;
  var mb = double.parse(covertToMegaByte.toStringAsFixed(2));
  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      backgroundColor: Colors.lightBlue.shade200,
      content: Text("The Size Of File Is $mb MB")));

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source