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

[Flutter] MobX in Flutter/Dart: How to design a "polymorphic" reactive model?

Discussão em 'Mobile' iniciado por Stack, Outubro 1, 2024 às 08:02.

  1. Stack

    Stack Membro Participativo

    how to design a "polymorphic" model? I have Meta with 2 children, FooMeta and BarMeta. I tried doing this:

    meta.dart:

    part 'meta.g.dart';

    class Meta extends _Meta with _$Meta {
    Meta({
    required super.key,
    super.label,
    });
    }

    abstract class _Meta with Store {
    String key;

    @observable
    String? label;

    _Meta({
    required this.key,
    this.label,
    });
    }


    foo_meta.dart:

    part 'foo_meta.g.dart';

    class FooMeta extends _FooMeta with _$FooMeta {
    FooMeta({
    required super.key,
    super.label,
    super.value,
    });
    }

    abstract class _FooMeta extends Meta {
    @observable
    String? value;

    _FooMeta({
    required super.key,
    super.label,
    this.value,
    });
    }


    But foo_meta.g.dart is never generated.

    My goal is to create a hierarchy of Meta that maps to polymorphic JSON. E.g. {"key": "Foo", "value": "Some string"} mapped to FooMeta while {"key": "Bar", "value": {"rich": "data"}} mapped to BarMeta}. In Java world, this is how you'd do it: https://stackoverflow.com/a/30386694

    Continue reading...

Compartilhe esta Página