19

I am using the following plugin: https://pub.dev/packages/freezed

I want to subclass a freezed data class to provide additional functionality in my data layer. So I have my data class which looks like:

import 'dart:ui';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'card.freezed.dart';

@freezed
abstract class Card with _$Card {
  factory Card({String text, Color color, List<String> categories}) = _Card;
}

Now I want to have this Card class as a super class to my CardModel so that the CardModel has access to the same fields, the copyWith method, value equality, ... But I have no Idea how to go about this. I am trying something like this:

import 'package:flutter/widgets.dart';
import 'package:growthdeck/features/card_deck/domain/entities/card.dart';

import '../../domain/entities/card.dart';

abstract class CardModel extends Card {
  factory CardModel.fromMap(Map<String, dynamic> card) => Card(
        text: card["text"],
        color: Color(int.parse(card['color'])),
        categories: card['categories'] as List<String>,
      );
}

Which throws the following error:

package:growthdeck/features/card_deck/data/models/card_model.dart 11:9  new CardModel.fromMap
test/features/card_deck/data/models/card_model_test.dart 13:23          main.<fn>

type '_$_Card' is not a subtype of type 'CardModel' in type cast

Is there any way to do this properly? My workaround would be to simply "wrap" the Card class inside the CardModel and provide a toCard() method which is not very elegant :S

8
  • 8
    Freezed classes are not made to be subclassed Aug 6, 2020 at 14:17
  • @RémiRousselet Is there a way to work around this issue? :S Or is the whole idea of what I am trying to do bad? Aug 6, 2020 at 15:30
  • and also is there a specific reason they are not made to be subclassed or simply because there was/is no need for them to be? Aug 6, 2020 at 15:42
  • 6
    No, there is no way to work around this issue. This is a requirement for copyWith to work properly. Use Object Composition instead of Inheritance Aug 6, 2020 at 16:32
  • 1
    @RémiRousselet alright thank you :) If you post this as an answer I can accept it :) Aug 7, 2020 at 13:17

3 Answers 3

11

Freezed doesn’t support inheritance at the moment so it’s recommended to use composition instead of inheritance as mentioned by the creator here:

https://github.com/rrousselGit/freezed/issues/464

And in the comments of the post.

0

Freezed class subclass itself. You dont need to abstract it.

import 'dart:ui';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'card.freezed.dart';

@freezed
class Card with _$Card {
 const factory Card.cardModel({String text, Color color, List<String> categories}) = _$CardModel;
}

Run build runner for above freeze will create

abstract class _$CardModel implements Card{}

which you can access by Card factory.

0

You can use dart_mappable. Here is an example for inheritance.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.