首页 > 解决方案 > Named parameter with underscore in Dart class

问题描述

From the Dart language tour you can do this:

class Person {
  final _name;

  const Person(this._name);
}

How can I get a named parameter like this:

class Person {
  final _name;

  const Person({@required this._name});
}

So the usage will be:

const Person(name: 'SD')

I still want a const constructor. Is this possible?

标签: dartflutter

解决方案


You can, but not with the this.variable syntax

const Person({ String name}) : _name = name;

推荐阅读