如何从基类初始化属性?

时间:2019-04-18 20:30:00

标签: flutter

我有许多扩展基类的类,但是在默认构造函数中初始化基类的成员时遇到了问题。

我尝试列出默认构造函数中的字段,带有和不带有“ this”。限定词。

我也遇到了类似的问题,未设置其他类中的字段,但是已通过放置“ this”进行了纠正。在田野前面。但是,如果我应用“ this”。对于继承的字段的限定词,我收到一条错误消息,指出这些字段不在封闭的类中。

基类:

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;
  }
}

我希望能够在默认构造函数中设置所有这些字段。

2 个答案:

答案 0 :(得分:0)

好吧,我相信您遇到问题的主要原因是您扩展了一个抽象类而不是实现它的事实。在extends类中将implements更改为Select。然后,您将遇到错误,因为您的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});

}

答案 1 :(得分:0)

您忘记了在继承的类中调用超级构造函数。这就是为什么未初始化超类成员的原因。 在您的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( ... )行中,我们调用超类构造函数,然后初始化超类成员。 您可以在其他继承的类中执行相同的操作。