The argument type 'Object?' can't be assigned to the parameter type 'FutureOr<Uint8List>?'

Issue

How to solve this error? Error message: The argument type ‘Object?’ can’t be assigned to the parameter type ‘FutureOr<Uint8List>?’.

enter image description here

My code

}

  Future<Uint8List> _getBlobData(html.Blob blob) {
    final completer = Completer<Uint8List>();
    final reader = html.FileReader();
    reader.readAsArrayBuffer(blob);
    reader.onLoad.listen((_) => completer.complete(reader.result));
    return completer.future;
  }

Solution

FileReader.result returns an Object? because it could return a Uint8List, a String, or null. If you can guarantee that reader.result is a Uint8List, just perform a cast:

reader.onLoad.listen((_) => completer.complete(reader.result! as Uint8List));

Otherwise you should check the type of reader.result first. For example:

reader.onLoad.listen((_) {
  var result = reader.result;
  if (result is Uint8List) {
    completer.complete(result);
  } else {
    completer.completeError(
      Exception('Unexpected result type: ${result.runtimeType}'));
  }
});

Answered By – jamesdlin

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.