Reputation: 57
I've got a number of classes that extend a base class, and I'm having problems getting the members from the base class initialized in the default constructor.
I have tried listing the fields in the default constructor, with and without the 'this.' qualifier.
I had similar issues with fields in other classes not being set, but that was corrected by placing 'this.' in front of the fields. However, if I apply the 'this.' qualifier to the inherited fields, I get an error saying the fields are not in the enclosing class.
Base class:
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});
}
Derived class:
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;
}
}
I would like to be able to set all of these fields in the default constructor.
Upvotes: 0
Views: 996
Reputation: 4741
You forgot to call super constructor in your inherited class. That is the reason why super class members has not been initialized.
In your Selection
class constructor you should do:
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
) ;
In : super( ... )
lines we call super class constructor and then the super class members are initialized.
You can do the same in others inherited classes.
Upvotes: 1
Reputation: 1461
Well, I believe that the main reason you're having problems is the fact that you're extending an abstract class instead of implementing it. Change extends
to implements
in the Select
class. Then you'll have errors because your Select
class needs to have and override the all properties in the abstract class.
Code:
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});
}
Upvotes: 0