Search code examples
flutterdartbase64assets

Convert image asset to base64 in Flutter


How to convert an image on my assets folder to base64 using Flutter?

My pubspec.yaml

assets:
 - assets/sticker_packs.wasticker
 - assets/WAStickersPack/

sticker_packs.wasticker is a json map to all .png images under assets/WAStickersPack/

I get files so

for (var item in stickers) {
  var file = 'assets/WAStickersPack/' + item["image_file"];

  var img = AssetImage(file);
  //or
  var imgto = await rootBundle.load(file);
}

But i dont know how to convert to base64


Solution

  • import 'package:flutter/services.dart';
    
    ByteData bytes = await rootBundle.load('assets/images/logo_splash.png');
    var buffer = bytes.buffer;
    var m = base64.encode(Uint8List.view(buffer));