schultek / dart_mappable

Improved json serialization and data classes with full support for generics, inheritance, customization and more.
https://pub.dev/packages/dart_mappable
MIT License
135 stars 20 forks source link

MappingHook with Future possible? #164

Closed llorenzo closed 4 months ago

llorenzo commented 5 months ago

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?

schultek commented 4 months ago

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.

llorenzo commented 4 months ago

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