我不确定为什么会失败?如何解决?
根据这个 https://dart.dev/tools/diagnostic-messages#private_optional_parameter 它被告知要使它们不带下划线。但这会使变量成为公共(public)变量而不是私有(private)变量?
class ContactElement extends StatelessWidget {
final IconData _icon;
final String _heading;
final String _describer;
const ContactElement({
Key? key,
required this._icon,
required this._heading,
required this._describer,
}) : super(key: key);
}
请帮忙
最佳答案
下划线表示 Dart 的私有(private)字段 ( more here )
class ContactElement extends StatelessWidget {
const ContactElement({
Key? key,
required IconData icon,
required String heading,
required String describer,
}) : _icon = icon,
_heading = heading,
_describer = describer,
super(key: key);
// Private fields
final IconData _icon;
final String _heading;
final String _describer;
// If you want that other parts of the code could to these
// fields, use getters and setters. Here is a getter example.
IconData get icon => _icon;
...
}
关于flutter - 命名参数不能以 null safe flutter 下划线开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69421872/