我有许多扩展基类的类,并且在从默认构造函数中初始化的基类中获取成员时遇到了问题。
我已经尝试在默认构造函数中列出字段,使用和不使用'this.‘限定符。
我在没有设置其他类中的字段时也遇到过类似的问题,但通过放置‘that’纠正了这个问题。在田野前面。但是,如果我应用“the”。限定符添加到继承的字段中时,我收到一个错误,指出这些字段不在包含的类中。
基类:
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(
{this.includedWhen,
this.includedIf,
this.mustIncludeWhen,
this.excludeWhen});
}派生类:
import 'package:randomizer/model/exclusiongroup.dart';
import 'package:randomizer/model/includable.dart';
import 'package:randomizer/model/includecondition.dart';
import 'package:randomizer/model/item.dart';
class Selection extends Includable {
String name;
int minNumToSelect;
int maxNumToSelect;
List<Item> items;
List<ExclusionGroup> exclusionGroups;
Selection(
{this.name,
this.items,
this.exclusionGroups,
this.minNumToSelect,
this.maxNumToSelect,
includedWhen,
includedIf,
mustIncludeWhen,
excludeWhen});
factory Selection.fromMap(Map<String, dynamic> map) {
Selection result = Selection(
name: map['name'] ?? '',
minNumToSelect:
map.keys.contains('minNumToSelect') ? map['minNumToSelect'] : 1,
maxNumToSelect:
map.keys.contains('maxNumToSelect') ? map['maxNumToSelect'] : 1,
items: map.keys.contains('items') ? Item.fromList(map['items']) : null,
exclusionGroups: map.keys.contains('exclusionGroups')
? ExclusionGroup.fromList(map['exclusionGroups'])
: null,
includedWhen: map.keys.contains('includedWhen')
? IncludeCondition.fromList(map['includedWhen'])
: null,
includedIf: map.keys.contains('includedIf')
? IncludeCondition.fromList(map['includedIf'])
: null,
mustIncludeWhen: map.keys.contains('mustIncludeWhen')
? IncludeCondition.fromList(map['mustIncludeWhen'])
: null,
excludeWhen: map.keys.contains('excludedWhen')
? IncludeCondition.fromList(map['excludedWhen'])
: null,
);
return result;
}
}我希望能够在默认构造函数中设置所有这些字段。
发布于 2019-04-19 05:27:44
好吧,我相信你有问题的主要原因是你扩展了一个抽象类,而不是实现它。在Select类中将extends更改为implements。然后,您将会遇到错误,因为您的Select类需要拥有并覆盖抽象类中的所有属性。
代码:
class Selection implements Includable {
String name;
int minNumToSelect;
int maxNumToSelect;
List<Item> items;
List<ExclusionGroup> exclusionGroups;
@override
List<IncludeCondition> excludeWhen;
@override
List<IncludeCondition> includedIf;
@override
List<IncludeCondition> mustIncludeWhen;
@override
List<IncludeCondition> excludeWhen;
Selection(
{this.name,
this.items,
this.exclusionGroups,
this.minNumToSelect,
this.maxNumToSelect,
this.includedWhen,
this.includedIf,
this.mustIncludeWhen,
this.excludeWhen});
}发布于 2019-04-19 09:56:07
你忘了在继承的类中调用超级构造函数。这就是超类成员没有被初始化的原因。在Selection类构造函数中,您应该执行以下操作:
Selection(
{this.name,
this.items,
this.exclusionGroups,
this.minNumToSelect,
this.maxNumToSelect,
// specify the type List<IncludeCondition> here is optional
List<IncludeCondition> includedWhen,
List<IncludeCondition> includedIf,
List<IncludeCondition> mustIncludeWhen,
List<IncludeCondition> excludeWhen}) : super(
includedWhen: includedWhen,
includedIf : includedIf
mustIncludeWhen : mustIncludeWhen,
excludeWhen : excludeWhen
) ;在: super( ... )行中,我们调用超类构造函数,然后初始化超类成员。您可以在其他继承的类中执行相同的操作。
https://stackoverflow.com/questions/55753335
复制相似问题