skip to Main Content

I wanted to know how I would convert the Image widget to base64 or bytes, because I took a clipping system and it delivers the result in Widget Image, I really need help to solve this problem.

I tried looking for Widget Image properties that could solve this, however, without success.
So I went looking for an answer on the internet and still I didn’t find any solution to this problem.

2

Answers


  1. import 'dart:convert';
    
    List<int> imageBytes = await widget.readAsBytes();
    print(imageBytes);
    String base64Image = base64Encode(imageBytes);
    

    This seems to work for me

    Login or Signup to reply.
  2. this works also but try others also which work and easy for you

    import 'dart:convert';
    import 'dart:io';
    import 'package:path/path.dart' as path;
    
    class ToImageData {
      static String imagetoData(String? imagepath) {
        final extension = path.extension(
          imagepath!.substring(imagepath.lastIndexOf("/")).replaceAll("/", ""),
        );
        final bytes = File(imagepath).readAsBytesSync();
        String base64 =
            "data:image/${extension.replaceAll(".", "")};base64,${base64Encode(bytes)}";
        return base64;
      }
    }
    

    to use is like this

    ToImageData.imagetoData(fileImageSelected.path)
    

    as per result would be a long string

    something like

    data:image/png;base64,UJYERWJKLDSJ1235612OLDSKHSJ
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search