我正在开发的Flutter应用程序中有以下课程。目的是要有一个可继承的类(Includable),该类由Item(以及一些其他类似的类,都产生相同的错误)扩展,并且具有可序列化的成员作为祖先类的一部分:
includedcondition.dart:
import 'package:json_annotation/json_annotation.dart';
part 'includecondition.g.dart';
@JsonSerializable()
class IncludeCondition {
String property;
String condition;
String selection;
String value;
IncludeCondition({this.property, this.condition, this.selection, this.value});
// Serialization Methods
factory IncludeCondition.fromJson(Map<String, dynamic> json) =>
_$IncludeConditionFromJson(json);
Map<String, dynamic> toJson() => _$IncludeConditionToJson(this);
}
includable.dart:
import 'package:randomizer/model/includecondition.dart';
import 'package:randomizer/model/setup.dart';
abstract class Includable {
List<IncludeCondition> includedWhen;
List<IncludeCondition> includedIf;
List<IncludeCondition> mustIncludeWhen;
List<IncludeCondition> excludeWhen;
Includable();
}
item.dart:
import 'package:randomizer/model/includable.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:randomizer/model/includecondition.dart';
part 'item.g.dart';
@JsonSerializable()
class Item extends Includable {
String name;
Item({this.name, this.expansion, this.playerMin, this.playerMax, this.cost});
// Serialization Methods
factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);
Map<String, dynamic> toJson() => _$ItemToJson(this);
}
构建项目时,我在item.g.dart类中获得“未定义名称'IncludeCondition'”,该代码具有以下代码:
Item _$ItemFromJson(Map<String, dynamic> json) {
return Item(
name: json['name'] as String,
..includedWhen = (json['includedWhen'] as List)
?.map((e) => e == null
? null
: IncludeCondition.fromJson(e as Map<String, dynamic>))
?.toList()
..includedIf = (json['includedIf'] as List)
?.map((e) => e == null
? null
: IncludeCondition.fromJson(e as Map<String, dynamic>))
?.toList()
..mustIncludeWhen = (json['mustIncludeWhen'] as List)
?.map((e) => e == null
? null
: IncludeCondition.fromJson(e as Map<String, dynamic>))
?.toList()
..excludeWhen = (json['excludeWhen'] as List)
?.map((e) => e == null
? null
: IncludeCondition.fromJson(e as Map<String, dynamic>))
?.toList();
}
如何摆脱这个错误?
答案 0 :(得分:1)
只需import 'includedcondition.dart;'
在您的item.dart
文件中。