3

I have a backend based on NodeJS and using mongodb as the database. Images with the field name photo is saved as object Type Buffer. I have successfully sent Images from the app using form data but I am not able to display the image in frontend.

This is the function used to get the data from API

Future<User> userWithId() async {
  User result;
   try {
      final response = await http.get(
        'api link',
        headers: <String, String>{
          'Authorization': 'Bearer $token',
        },
      );
      if (response.statusCode == 200) {
        result = User.fromJson(jsonDecode(response.body));
      }
   } catch (e) {
      print(e.toString());
   }
  return result;
}

This is the fromJson function of the class User. The photo field here returns the image as buffer.

factory User.fromJson(Map<String, dynamic> json) {
  return User(
    id: json['_id'] as String ?? "",
    email: json['email'] as String ?? "",
    // profilePhoto: json["photo"] ?? null,
    // profilePhoto: base64.encode(jsonDecode(json["photo"])) ?? null,
  );
}

This is the data from the photo field enter image description here

This is the data from MongoDB database enter image description here

Nehal
  • 1,261
  • 12
  • 18

6 Answers6

1

For those who are facing this issue as well, this is a multi-part problem:

  1. Converting List to List.
  2. Using converted List to read/display image.

In OP's case, to get buffer List would be profilePhoto: json["photo"]["data"]["data"].

For step 1, To convert List to List:

List<dynamic> bufferDynamic = profilePhoto: json["photo"]["data"]["data"];
List<int> bufferInt = buffer.map((e) => e as int).toList();

For step 2 as some have already mention, use Image.memory class provided by Flutter.

Image.memory(Uint8List.fromList(bufferInt))

Hope this helps for those who need to read/display image from buffer. (:

RyanNa
  • 119
  • 1
  • 6
0

you can use base64Decode method from dart:convert

store your image binary in string format:

factory User.fromJson(Map<String, dynamic> json) {
  return User(
    ...
    profilePhoto: json["photo"] ?? null,
    ...
  );
}

and use the following code in UI:

Image.memory(base64Decode(user.profilePhoto))

also, don't forget to add an if statement to check if the photo is null or not

Hope, it helps

iamdipanshus
  • 492
  • 3
  • 12
  • ````type '_InternalLinkedHashMap' is not a subtype of type 'String'```` This error shows up when using ````json["photo"]```` While using ````json["photo"]['data']['data']```` this error shows up ````type 'List' is not a subtype of type 'String'```` – Nehal Feb 26 '21 at 12:56
0

json['photo']['data']['data']; By doing this you are getting this error List' is not a subtype of type 'String'. So may be your return type for profilePhoto is String. Change it to dynamic then try again.

Shriya Pandya
  • 404
  • 3
  • 12
0

Thanks to the great article bellow explaining about bytes in dart, you can convert your response data which is as List of integers, to Uint8List data type and pass it to Image.memory to render image.

Image.memory(Uint8List.fromList(// pass image data array here));

https://medium.com/flutter-community/working-with-bytes-in-dart-6ece83455721

Alireza Abiri
  • 512
  • 4
  • 11
0

Try like this

List<int> imageData = user.profilePhoto['data'] as List<int>
////
Image.memory(Uint8List.fromList(imageData));
Taz
  • 1,737
  • 1
  • 15
  • 30
  • 1
    I try that but ══╡ 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:5275:5) #1 ImageDescriptor.encoded (dart:ui/painting.dart:5143:12) #2 instantiateImageCodec (dart:ui/painting.dart:1999:60) – vannak Apr 25 '21 at 08:00
0

Image type

You should modify your class User, currently you have something like:

class User {
  final String id;
  final String email;
  final ? profilePhoto
}

I am not sure which type you are currently using but you need to change it accordingly to the data you get in your response. Your image is, in your computer, a list of bytes (uint8, meaning unsigned integer of 8 bits, so 1 byte), ranging from 0 to 255 in value (0 to 0xFF).


Images header

You can read about EXIF to understand better how the standard specifies the formats for images

Every JPEG file starts from binary value '0xFFD8', ends by binary value '0xFFD9'.

The first eight bytes of a PNG file always contain the following (decimal) values: 137 80 78 71 13 10 26 10


Fix

Change the type of your profilePhoto to Uint8List, full code:

class User {
  final String id;
  final String email;
  String profilePhoto // I don't know which type you use

  User({this.id, this.email, this.profilePhoto});

  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      id: json['_id'] as String ?? "",
      email: json['email'] as String ?? "",
      profilePhoto: json["photo"] ?? null,
    );
  }
}

And then load the image in your widget with Image.memory while being cautious as its value could be null:

return user.profilePhoto != null ? Image.memory(user.profilePhoto) : Container();

NB: Image.memory is a shorthand of an Image widget backed by MemoryImage.

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81