Home > Blockchain >  There is a problem converting the unint8list image in Flutter
There is a problem converting the unint8list image in Flutter

Time:08-06

I am working on uploading the uint8List type image to Firestore and get it again. I have converted to String to upload to Firestore.

  Future _pageDrawScreen() async {
    // screenshot pakage
    Uint8List? _previewImage = await _testController.capture(); // get uint8List image data
    if (_previewImage == null) {
      logger.d("_previewImage null");
    }

    // convert to String
    _pageModel!.previewImage = _previewImage.toString();

   // Firestore update
  }

Then, I take the image data again, convert it, and output it as a memory image.

  Widget _pageContainer(int index, PageModel pageModel) {
    // get image and convert
    Uint8List? previewImage;

    if (pageModel.previewImage != null) {
      List<int> list = pageModel.previewImage!.codeUnits;
      previewImage = Uint8List.fromList(list);
      // logger.d(previewImage);
    }

    return ListTile(
      title: pageModel.previewImage != null
          ? ExtendedImage.memory(
              previewImage!,// error!
              width: 125,
              height: 125,
              fit: BoxFit.cover,

              // check image state
              loadStateChanged: (state) {
                switch (state.extendedImageLoadState) {
                  case LoadState.loading:
                    return const SizedBox(
                      width: 50,
                      height: 50,
                      child: CircularProgressIndicator(
                        color: Colors.red,
                      ),
                    );

                  case LoadState.completed:
                    return null;

                  // Failed error!!
                  case LoadState.failed:
                    return const Material(
                      color: Colors.white,
                      child: Icon(
                        Icons.cancel,
                        color: Colors.red,
                        size: 50,
                      ),
                    );
                }
              },
            )
          : Container(
              width: 125,
              height: 125,
              color: Colors.white,
              child: const Center(
                child: CircularProgressIndicator(),
              ),
            ),
    );
  }

However, this image is checked as an error state. Is the conversion of Uint8List wrong?

Expected Results (When using the uint8list image as it is, ):

enter image description here

Actual Results:

enter image description here

CodePudding user response:

import

import 'dart:convert';

for uploading

Uint8List? _previewImage = await _testController.capture();
if(_previewImage != null){
    String previewImage = base64.encode(_previewImage);
}

after fetching the image data

Uint8List previewImage = base64.decode(previewImage);
  • Related