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...
4 Answers
Use this for your image widget: Image.memory(bytes). You can find more documentation on the Flutter dev website.
2 Comments
StackDrenny
Thank you I must of skipped over this when looking.
ch271828n
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."
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
Mohsen Emami
getNextFrame() method is inaccessible in Flutter 2.0.5Container(
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
Jeremy Caney
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?
Breno Veríssimo
He never said it was a base64 data