Search code examples
androidflutterdartcross-platform

Future<Uint8List> to Base64 or PNG in Flutter


I need to convert a Future<Uint8List> to Base64 or PNG in Flutter, I am using this pub to get signature and export it but when I call toPngBytes() method (method in pub) it returns a Future<Uint8List> and I need to convert it to Base64 format or List<int> at least ByteData format, I can not convert it to a more usable format for me, can anyone help me to resolve this.

_controller.toPngBytes(); // _controller is a variable that holds info about my signature.

Solution

  • import 'dart:convert';
    
    // async variant
    final imageData = await _controller.toPngBytes(); // must be called in async method
    final imageEncoded = base64.encode(imageData); // returns base64 string
    
    // callback variant
    _controller.toPngBytes().then((data) {
     final imageEncoded = base64.encode(data); 
    });