How can I reference a generic object that implements JsonSerialized with freezed in dart

765 Views Asked by At

I'm using https://pub.dev/packages/freezed to generate data classes with json.

I want to do this, so that I can use toJson() within the class.

class DatabaseRepository<T extends JsonSerializable> { ... }

But when I define the generic, I get the error: 'Prompt' doesn't conform to the bound 'JsonSerializable' of the type parameter 'T'. Try using a type that is or is a subclass of 'JsonSerializable' enter image description here

Prompt is defined like so:

import 'package:freezed_annotation/freezed_annotation.dart';

part 'prompt.freezed.dart';
part 'prompt.g.dart';

@freezed
class Prompt with _$Prompt {
  const factory Prompt({
    required String text,
    String? id,
    @Default(false) bool archived,
  }) = _Prompt;

  factory Prompt.fromJson(Map<String, dynamic> json) => _$PromptFromJson(json);
}

Is JsonSerializable the wrong class to extend? Or is this not possible to do?

1

There are 1 best solutions below

0
On

The JsonSerializable is used as an annotation, not as a base class. Therefore it’s not extended so that it is not suitable as a type bound.

As an alternative you could however create an abstract class providing the definition of the toJson method, let your Prompt implement it and use that as a generic type bound, e.g.

abstract class JsonObject {
    Map<String, dynamic> toJson();
}

@freezed
class Prompt with _$Prompt implements JsonObject {
    …
    Map<String, dynamic> toJson() => _$PromptToJson(this);
    …

}

class DatabaseRepository<T extends JsonObject> { … }

See also How to enforce Implements constraint on generic type T in generic Dart method?.