Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

849
Views
Convert Image from Buffer to display in Frontend (Flutter)

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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

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

over 4 years ago · Santiago Trujillo Report

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.

over 4 years ago · Santiago Trujillo Report

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!