flutter - 命名参数不能以 null safe flutter 下划线开头

标签 flutter dart

我不确定为什么会失败?如何解决?

根据这个 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/

相关文章:

flutter - 在Flutter中,我想通过按按钮获取当前日期并在Textfield中显示,该怎么办?

flutter - 是否可以强制“文本”小部件使用两个行空间?

dart - Dart-运行RPC生成客户端时出错

flutter - 当我在 Flutter 中打印 map 时,Flutter 中缺少双引号

dart - Flutter CachedNetworkImageProvider - 如何在出错时显示本地默认图像?

asynchronous - Flutter:从 Future 获取值(value)的正确方法

flutter - 设置脚手架背景色

webview - Flutter WebView 插件无法播放某些 YouTube 视频

dart - 从 Dart 中的字符串中删除 HTML 标签

Flutter 为 Web 构建 - "Failed to compile application"