12

I want to get a image from my REST API service however haven't found any documentation on how to decode the response body that would be a byte array to an image in Flutter? Anyone with some useful resources, please help...

0

4 Answers 4

15

Use this for your image widget: Image.memory(bytes). You can find more documentation on the Flutter dev website.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I must of skipped over this when looking.
Hi how can I decode raw image byte array? that function says "This only accepts compressed image formats (e.g. PNG). Uncompressed formats like rawRgba (the default format of [ui.Image.toByteData]) will lead to exceptions."
12

Since top rated answer use flutter/widget.dart, this use dart:ui only

Future<Image> tinypng() async {
  final bytes = Uint8List.fromList([
    137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0,
    1, 0, 0, 0, 1, 8, 6, 0, 0, 0, 31, 21, 196, 137, 0, 0, 0, 10, 73, 68, 65,
    84, 120, 156, 99, 0, 1, 0, 0, 5, 0, 1, 13, 10, 45, 180, 0, 0, 0, 0, 73,
    69, 78, 68, 174, 66, 96, 130 // prevent dartfmt
  ]);

  // copy from decodeImageFromList of package:flutter/painting.dart
  final codec = await instantiateImageCodec(bytes);
  final frameInfo = await codec.getNextFrame();
  return frameInfo.image;
}

1 Comment

getNextFrame() method is inaccessible in Flutter 2.0.5
7

This will return a Widget

Image.memory(Uint8List);

Comments

0
Container(
    height: MediaQuery.of(context).size.height * .2,
    width: MediaQuery.of(context).size.width * .9,
  
  child:Image.memory(
    base64.decode(controlleR.offersList[index].pictureEn.toString()),
  ),
);

2 Comments

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?
He never said it was a base64 data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.