skip to Main Content

Help me please, I have saved in sqlite image Uint8List as TEXT in database, now i want to display it and i dont know how to fetch with future builder. I have DBHElper with function, but dont know how to get that image and show it with FutureBuilder. Flutter

getFromGallery(ImageSource source) async {
    final ImagePicker imagePicker = ImagePicker();

    XFile? file = await imagePicker.pickImage(source: source);

    if (file != null) {
      return await file.readAsBytes();
    }
  }

  void selectImage() async {
    Uint8List imageFile = await getFromGallery(ImageSource.gallery);
    setState(() {
      _image = imageFile;
    });
  }

This is the function that saves the image to sqlite

2

Answers


  1. save as TEXT like

    String imageText = String.fromCharCodes(_image);
    

    and convert back to

    var imageFile = Uint8List.fromList(imageText.codeUnits);
    
    Login or Signup to reply.
  2. class ImageDisplayWidget extends StatelessWidget {
      final int imageId;
      final DBHelper dbHelper = DBHelper();
    
      ImageDisplayWidget({required this.imageId});
    
      @override
      Widget build(BuildContext context) {
        return FutureBuilder<Uint8List>(
          future: dbHelper.getImage(imageId),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Image.memory(snapshot.data!);
            } else {
              return Placeholder();
            }
          },
        );
      }
    }
    

    In this example, DBHelper is assumed to have a method called getImage that takes an imageId parameter and returns a Future that retrieves the image data from the database. The ImageDisplayWidget takes an imageId parameter and uses FutureBuilder to asynchronously retrieve and display the image data.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search