-1

I'm making a watermark image using flutter and I need to convert Future to Uint8List.

This is the code where the problem happens.

child: Image.memory(putWatermarkOnImage(asset))

The child is in the Container widget and the result of the putWatermarkOnImage function has to be Uint8List type not a future type.

Future<Uint8List> putWatermarkOnImage(asset) async {
  final assetFile = await asset.file;
  var imageBytes = await assetFile!.readAsBytes();
  ByteData watermarkImgByteData = await rootBundle.load('assets/images/ournow_logo.png');
  Uint8List watermarkImgBytes = watermarkImgByteData.buffer.asUint8List();
  Uint8List imageUint8List = Uint8List.fromList(imageBytes);
  Uint8List watermarkedImg = await ImageWatermark.addImageWatermark(
      originalImageBytes: imageUint8List,
      waterkmarkImageBytes: watermarkImgBytes,
      imgHeight: 200,
      imgWidth: 200,
      dstY: 400,
      dstX: 400);
  return watermarkedImg;
}

How can I solve this problem?

황희윤
  • 441
  • 6
  • 14

1 Answers1

0

I have taken your code and wrote it the way I do to avoid confusions, so I created a class for you on the side to collect all bytes transformation related methods and called them "BytesHandlers"

then I re-organized the code as follows,,, I believe it will work please test it and tell me if something is going wrong

import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

Future<Uint8List> putWatermarkOnImage(AssetEntity asset) async {
  Uint8List _output;

  if (asset != null){

    final File assetFile = await asset.file;

    final ByteData watermarkImgByteData = await BytesHandlers.getByteDataFromPath('assets/images/ournow_logo.png');

    _output = await BytesHandlers.getUint8ListFromFile(assetFile);

    _output = await ImageWatermark.addImageWatermark(
        originalImageBytes: _output,
        waterkmarkImageBytes: watermarkImgByteData,
        imgHeight: 200,
        imgWidth: 200,
        dstY: 400,
        dstX: 400
    );

  }

  return _output;
}

class BytesHandlers {
  // --------------------
  BytesHandlers();
  // --------------------
  static Future<ByteData> getByteDataFromPath(String assetPath) async {
    /// NOTE : Asset path can be local path or url
    ByteData _byteData;

    if (assetPath != null){
      _byteData = await rootBundle.load(assetPath);
    }

    return _byteData;
  }
  // --------------------
  static Future<Uint8List> getUint8ListFromFile(File file) async {
    Uint8List _uInt;

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

then you get to call it like this

final Uint8List _ImageWithWatermark = await putWatermarkOnImage(_inputFile);
Rageh Azzazy
  • 701
  • 1
  • 7
  • 16
  • I tried your code, but the problem still exists and saying that The argument type 'Future' can't be assigned to the parameter type 'Uint8List' – 황희윤 Nov 08 '22 at 02:56
  • Where exactly for which line does it say that ? and are you sure when you call this method you put it with await ? like this final Uint8List _ImageWithWatermark = await putWatermarkOnImage(_inputFile); – Rageh Azzazy Nov 08 '22 at 08:18