1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] json_serializable: Selecting JsonConverter's at runtime

Discussão em 'Mobile' iniciado por Stack, Outubro 17, 2024 às 12:13.

  1. Stack

    Stack Membro Participativo

    I'm looking for a solution on how I could switch what JsonConverter I am using at runtime with the package:json_serializable package. This will allow my app use DateTime objects but then convert these to/from Firestore Timestamp objects only when required (when communicating with Firestore).

    I need a solution that would allow me to share the data models in none-flutter/firestore environments and they just work as expected as DateTime objects.

    Current solution (built_value)


    My app that shares it's data models between flutter apps, Dart-SDK only environments (GCP Cloud Run) and firestore. I currently use package:built_value and I have two sets of serializers (dartSerializers & firestoreSerializers). This allows me to choose which I use, see example below:

    FooBar.fromJsonMap(
    Serializers serializers, Map<String, dynamic> data) =>
    serializers.deserializeWith(ChatMessage.serializer, data)!;

    Map<String, dynamic> toJsonMap(Serializers serializers) =>
    Map.of(serializers.serialize(this, //....



    Thanks

    Continue reading...

Compartilhe esta Página