提问人:Omega500 提问时间:11/13/2023 最后编辑:jamesdlinOmega500 更新时间:11/20/2023 访问量:30
Isar 生成器构造函数参数类型与属性类型不匹配时使用 super
Isar Generator Constructor parameter type does not match property type when using super
问:
我正在为我的项目使用干净的架构,并希望将数据库添加到我的应用程序中,所以我决定使用 isar 数据库,但由于干净的架构,我需要我的 isar 集合仅在模型上而不是实体上,但是当我尝试在模型上创建集合并使用 isar 生成器为其生成代码时,我一直收到此错误
这是错误
[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
这是我第一次使用数据库,所以我不知道为什么会这样。
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)''';
}
}
答: 暂无答案
评论