'Converting a byte array to image in Flutter?
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...
Solution 1:[1]
Use this for your image widget: Image.memory(bytes) . You can find more documentation here: https://docs.flutter.io/flutter/widgets/Image/Image.memory.html
Solution 2:[2]
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;
}
Solution 3:[3]
This will return a Widget
Image.memory(Uint8List);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | C-Spydo |
Solution 2 | Ken |
Solution 3 | balu k |