How to get a Flutter Uint8List from a Network Image? How to get a Flutter Uint8List from a Network Image? dart dart

How to get a Flutter Uint8List from a Network Image?


The simplest way seeems to get the http response using the image url and response.bodyBytes would contain the data in Uint8List.

http.Response response = await http.get(    'https://flutter.io/images/flutter-mark-square-100.png',);   response.bodyBytes //Uint8List

Now you can do things like converting to base64 encoded string base64.encode(response.bodyBytes);
Update: With newer version of http, you need to add Uri.parse()
Eg.

http.Response response = await http.get(    Uri.parse('https://flutter.io/images/flutter-mark-square-100.png'),);


  void initState() {    super.initState();    var sunImage = new NetworkImage(        "https://resources.ninghao.org/images/childhood-in-a-picture.jpg");    sunImage.obtainKey(new ImageConfiguration()).then((val) {      var load = sunImage.load(val);      load.addListener((listener, err) async {        setState(() => image = listener);      });    });  }

See also https://github.com/flutter/flutter/issues/23761#issuecomment-434606683

Then you can use image.toByteData().buffer.asUInt8List()

See also https://docs.flutter.io/flutter/dart-ui/Image/toByteData.html


The answers here are relevant and help explain how dart and flutter image compression/conversion works. If you would like a shortcut, there is this package https://pub.dev/packages/network_image_to_byte that makes it really easy.