private named parameter in dart

Issue

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 _?

Solution

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, List<Character> characterList})
  : _characterList  characterList,
    super(key: key);

final List<Character> _characterList;

Answered By – jamesdlin

Leave a Comment