From backend server, I received buffer array like belows.
{
"type": "Buffer",
"data": [
239,
191,
189,
80,
78,
71,
13,
10,
26,
...
]
}
To receive above data, I created a response DTO like this.
@freezed
class FileDownloadResponse with _$FileDownloadResponse {
factory FileDownloadResponse({
/// file type
@JsonKey(name: 'type') required String type,
/// buffer
@JsonKey(name: 'data') required List<int> imageBuffer,
}) = _FileDownloadResponse;
factory FileDownloadResponse.fromJson(Map<String, dynamic> json) => _$FileDownloadResponseFromJson(json);
}
And, with that imageBuffer, I tried to make a Image.memory widget like this. (For easy description, I simplified my code, and checked that profileImageUrl value is not null)
class SelectProfileWidget extends StatelessWidget {
final List<int>? imageBuffer;
final double? height;
final double? width;
final double? iconHeight;
final double? iconWidth;
SelectProfileWidget({
Key? key,
this.imageBuffer,
this.height,
this.width,
this.iconHeight,
this.iconWidth,
}) : super(key: key);
@override
Widget build(BuildContext context) {
if (imageBuffer != null) {
return Image.memory(
Uint8List.fromList(imageBuffer!),
height: height ?? 88,
width: width ?? 88,
fit: BoxFit.cover,
);
}
return const SizedBox.shrink();
}
}
When I tried to run this code, this error was occurred.
E/FlutterJNI( 3742): Failed to decode image
E/FlutterJNI( 3742): android.graphics.ImageDecoder$DecodeException: Failed to create image decoder with message 'unimplemented'Input contained an error.
E/FlutterJNI( 3742): at android.graphics.ImageDecoder.nCreate(Native Method)
E/FlutterJNI( 3742): at android.graphics.ImageDecoder.access$200(ImageDecoder.java:173)
E/FlutterJNI( 3742): at android.graphics.ImageDecoder$ByteBufferSource.createImageDecoder(ImageDecoder.java:250)
E/FlutterJNI( 3742): at android.graphics.ImageDecoder.decodeBitmapImpl(ImageDecoder.java:1847)
E/FlutterJNI( 3742): at android.graphics.ImageDecoder.decodeBitmap(ImageDecoder.java:1840)
E/FlutterJNI( 3742): at io.flutter.embedding.engine.FlutterJNI.decodeImage(FlutterJNI.java:524)
======== Exception caught by image resource service ================================================
The following _Exception was thrown resolving an image codec:
Exception: Invalid image data
When the exception was thrown, this was the stack:
#0 _futurize (dart:ui/painting.dart:5718:5)
#1 ImageDescriptor.encoded (dart:ui/painting.dart:5574:12)
#2 instantiateImageCodec (dart:ui/painting.dart:2056:60)
<asynchronous suspension>
====================================================================================================
What is the problem in my code...? Or, is there another way to convert buffer array to image widget??
Please help me