0

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?

HJo
  • 1,902
  • 1
  • 19
  • 30
  • You can have `toJson()` method with both generators `Freezed` generate automatically `toJson()` method if you add `fromJson()`. With `json_serializable` you need to add manually `toJson()` method. – Arnas Mar 31 '22 at 16:58
  • I know, my question is how can I conform to a class using extends keyword – HJo Apr 01 '22 at 10:20

1 Answers1

0

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?.

Thomas
  • 11
  • 2