Isar Generator Constructor parameter type does not match property type when using super

223 views Asked by At

I am using clean architecture for my project and want to add a database to my application so I decided to use isar database but because of clean architecture I need my isar collections to be only on models and not entities but when I was trying to create a collection on a model and was generating the code for it using isar generator I kept getting this error

here's the error

[INFO] Generating build script completed, took 875ms
[INFO] Reading cached asset graph completed, took 231ms
[INFO] Checking for updates since last build completed, took 3.3s
[SEVERE] isar_generator on lib/data/models/plugin.dart:

Constructor parameter type does not match property type
package:meiyou/data/models/plugin.dart:24:11
   ╷
24 │     super.dependencies,
   │           ^^^^^^^^^^^^
   ╵
[INFO] Running build completed, took 5.2s
[INFO] Caching finalized dependency graph completed, took 181ms
[SEVERE] Failed after 5.4s

It's my first time using a database so I don't know why this is happening.

import 'dart:convert';
import 'package:meiyou/core/utils/extenstions/iterable.dart';
import 'package:meiyou/core/utils/from_entity.dart';
import 'package:meiyou/domain/entities/plugin.dart';
import 'package:isar/isar.dart';
part 'plugin.g.dart';

@collection
class Plugin extends PluginEntity {
  final Id id;

  @override
  List<Dependency>? get dependencies =>
      super.dependencies?.mapAsList<Dependency>(
          (it) => it is Dependency ? it : Dependency.fromEntity(it));

  const Plugin({
    this.id = Isar.autoIncrement,
    required super.name,
    required super.source,
    required super.version,
    super.icon,
    super.info,
    super.dependencies,
  });

  factory Plugin.fromEntity(PluginEntity entity) {
    return createFromEntity<PluginEntity, Plugin>(
        entity,
        Plugin(
          name: entity.name,
          source: entity.source,
          version: entity.version,
          icon: entity.icon,
          info: entity.info,
          dependencies: entity.dependencies?.mapAsList<Dependency>(
              (it) => it is Dependency ? it : Dependency.fromEntity(it)),
        ));
  }

  factory Plugin.decode(String jsonString) =>
      Plugin.fromJson(jsonDecode(jsonString));

  factory Plugin.fromJson(dynamic json) {
    return Plugin(
      name: json['name'],
      source: json['source'],
      version: json['version'],
      icon: json['icon'],
      info: json['info'],
      dependencies: (json['dependencies'] as List?)
          ?.mapAsList((it) => Dependency.fromJson(it)),
    );
  }

  String get encode => jsonEncode(toJson());

  Map<String, dynamic> toJson() {
    return {
      "name": name,
      "source": source,
      "version": version,
      "icon": icon,
      "info": info,
      "dependencies": dependencies?.mapAsList((e) => e.toJson()),
    };
  }

  Plugin copyWith({
    String? name,
    String? source,
    String? version,
    String? icon,
    String? info,
    List<Dependency>? dependencies,
  }) {
    return Plugin(
      name: name ?? this.name,
      source: source ?? this.source,
      version: version ?? this.version,
      icon: icon ?? this.icon,
      info: info ?? this.info,
      dependencies: dependencies ?? this.dependencies,
    );
  }

  @override
  String toString() {
    return '''Plugin(name: $name, source: $source, version: $version, icon: $icon, info: $info, dependencies: $dependencies)''';
  }
}

@embedded
class Dependency extends DependencyEntity {
  final Id id;
  const Dependency({
    this.id = Isar.autoIncrement,
    required super.name,
    required super.source,
    required super.version,
  });

  factory Dependency.fromEntity(DependencyEntity entity) {
    return Dependency(
      name: entity.name,
      source: entity.source,
      version: entity.version,
    );
  }

  factory Dependency.decode(String jsonString) =>
      Dependency.fromJson(jsonDecode(jsonString));

  factory Dependency.fromJson(dynamic json) {
    return Dependency(
      name: json['name'],
      source: json['source'],
      version: json['version'],
    );
  }

  String get encode => jsonEncode(toJson());
  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'source': source,
      'version': version,
    };
  }

  @override
  String toString() {
    return '''Dependency(name: $name, source: $source, version: $version)''';
  }
}

0

There are 0 answers