Skip to content

Return null instead of a class annotated with MappableClass.useAsDefault, when there is an unknown discriminator value #293

@saibotma

Description

@saibotma

It would be great to be able to configure dart_mappable, so that it returns null instead of throwing an exception or returning the class annotated with MappableClass.useAsDefault, when there is an unknown discriminator value.

Basically, it should do the same as this example does:

import 'dart:convert';

void main(List<String> arguments) {
  final json = '{"type": "start"}';
  final type = jsonDecode(json)["type"];
  final event = switch (type) {
    "start" => Start(),
    "end" => End(),
    _ => null,
  };
}

sealed class Event {}
class Start implements Event {}
class End implements Event {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions