Reputation: 309
I am new to Dart. Let's say I have a final List <Character> _characterList;
which should be private. But how do I use CharacterList ({Key key, this._characterList}): super (key: key);
if the named parameter cannot start with _?
Upvotes: 8
Views: 4563
Reputation: 90005
The name of the parameter is independent of the name of the member. Constructors offer the this.name
syntactic sugar for convenience if the names happen to be the same, but you don't have to use that. You could give the parameter its own name and explicitly initialize the member variable separately:
CharacterList({Key? key, required List<Character> characterList})
: _characterList = characterList,
super(key: key);
final List<Character> _characterList;
Upvotes: 15
Reputation: 3007
It is Dart limitation
Named optional parameters can't start with an underscore
You can use a constructor with optional positional parameter instead
CharacterList (Key key, [this._characterList]): super (key: key)
Upvotes: 1