How to display an Image.memory

DavidY picture DavidY · Sep 10, 2019 · Viewed 7.5k times · Source

I need to display an Image.memory in my project, but when I put this code in the body of Scaffold, it doesn't show.

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'dart:convert';



Codec<String, String> stringToBase64 = utf8.fuse(base64);
String imagenJson = "Here, I put the BASE64 string";
Uint8List _image = base64Decode(imagenJson);
Image.memory(_image);

return Scaffold(
  appBar: AppBar(
    title: Text('Pruebas BASE64'),
  ),
  body: Image.memory(_image)
);

Answer

CopsOnRoad picture CopsOnRoad · Sep 10, 2019
// Create an instance variable
Image image;

@override
void initState() {
  super.initState();

  // put them here
  Codec<String, String> stringToBase64 = utf8.fuse(base64);
  String imagenJson = "Here, I put the BASE64 string";
  Uint8List _image = base64Decode(imagenJson);
  image = Image.memory(_image); // assign it value here

}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(),
    body: image, // use it here
  );
}