schultek/dart_mappable

MappingHook with Future possible?

Closed this issue · 2 comments

When serialising an Image to be part of the json I need to use an async Future:

  @override
  Future<Object>? beforeEncode(Object? value) async {
      var file = XFile(value.toString());
      List<int> fileBytes = await file.readAsBytes();
      return base64.encode(fileBytes);
  }

It throws an error. => Unknown type _Future. Did you forgot to annotate the class or register a custom mapper?

Generally everything will only execute synchronously. When you return a future, that future would be treated as the encoded value instead of awaited. And since there is no mapper for Future, it fails.

You could add a custom mapper for Future but I don't know if that would help you since again the future can't be awaited during the serialization process since everything is synchronous.

I now serialize the images beforehand. This, it works for me. Thank you