如何在构造函数主体中初始化不可为空的成员? [英] How do I initialize non-nullable members in a constructor body?

查看:334
本文介绍了如何在构造函数主体中初始化不可为空的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用这种方式在Dart中创建了我的类,但是我得到了不可空实例字段'text'必须初始化.尝试添加一个初始化程序表达式,或在此构造函数中添加一个字段初始化程序,或将其标记为"late".有可能,谢谢你.

I've created my class in Dart this way, but I'm getting the Non-nullable instance field 'text' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'. I would like to know if there's a way to do it in a 'Python' style where this kind of class creation is possible, thank you in advance.

class Lexer {
  String _text;
  int _pos;
  String _current_char;

  Lexer(String text) {
    this._text = text;
    this._pos = -1;
    this._current_char = '';
    this.advance();
  }

  void advance() {
    this._pos++;
    this._current_char = this._pos < this._text.length ? this._text[this._pos] : '';
  }
}

推荐答案

class Lexer {
  String _text;
  int _pos;
  String _current_char;

这将声明几个类型为 String 的成员.由于它们被声明为 String 而不是 String?,因此这些成员是不可为空的;它们不允许为 null .(这是Dart 2.12中新的null安全功能的一部分.)

This declares several members with type String. Since they are declared as String and not as String?, these members are non-nullable; they are not allowed to ever be null. (This is part of the new null-safety feature from Dart 2.12.)

  Lexer(String text) {
    this._text = text;
    this._pos = -1;
    this._current_char = '';
    this.advance();
  }

Dart分两个阶段初始化对象.当构造函数的 body 运行时,Dart希望所有成员变量都已初始化.因为您的成员是不可为空的,并且尚未初始化为非null值,所以这是一个错误.错误消息说明了您可以做什么:

Dart initializes objects in two phases. When the constructor's body runs, Dart expects all member variables to already be initialized. Because your members are non-nullable and haven't been initialized to non-null values yet, this is an error. The error message explains what you can do:

非空实例字段"text"必须初始化.尝试添加一个初始化程序表达式,或在此构造函数中添加一个字段初始化程序,或将其标记为"late".

Non-nullable instance field 'text' must be initialized. Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'.

  • 使用初始化程序表达式.这意味着使用 初始化器列表 :

    Lexer(String text)
      : _text = text,
        _pos = -1,
        _current_char = '' {
      advance();
    }
    

  • 添加字段初始化程序.这意味着在类声明中初始化成员 inline .

    class Lexer {
      String _text = '';
      int _pos = -1,
      String _current_char = '';
    

  • 将您的成员标记为 late .这意味着您保证在任何尝试使用变量之前都将对其进行初始化.

  • Marking your members as late. This means that you promise that the variables will be initialized before anything attempts to use them.

    class Lexer {
      late String _text;
      late int _pos,
      late String _current_char;
    

  • 使您的成员可以为空,这默认情况下允许它们隐式为空:

  • Making your members nullable, which allows them to be implicitly null by default:

    class Lexer {
      String? _text;
      int? _pos,
      String? _current_char;
    

    但是,我不建议这样做,因为这将要求所有访问在使用成员之前明确检查成员是否不为空.

    However, I don't recommend that since that will require that all accesses explicitly check that the members aren't null before using them.

    您还可能需要阅读:是否可以立即或在构造函数中将变量分配给变量?

    这篇关于如何在构造函数主体中初始化不可为空的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆