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

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 1:[1]

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}'));
  }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jamesdlin