从sqlite数据库获取图像flutter

kcwpcxri  于 2023-04-06  发布在  SQLite
关注(0)|答案(2)|浏览(174)

请帮助我,我已经保存在sqlite图像Uint8List作为文本在数据库中,现在我想显示它,我不知道如何与未来的建设者获取.我有DBHElper与功能,但不知道如何获得该图像,并显示它与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;
    });
  }

这是将图像保存到sqlite的函数

o75abkj4

o75abkj41#

保存为类似文本

String imageText = String.fromCharCodes(_image);

并转换回

var imageFile = Uint8List.fromList(imageText.codeUnits);
gfttwv5a

gfttwv5a2#

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();
        }
      },
    );
  }
}

在此示例中,假设DBHelper具有一个名为getImage的方法,该方法接受imageId参数并返回一个Future,该Future从数据库中检索图像数据。ImageDisplayWidget接受imageId参数并使用FutureBuilder异步检索和显示图像数据。

相关问题