Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

dart - How do I initialize non-nullable members in a constructor body?

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] : '';
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
class Lexer {
  String _text;
  int _pos;
  String _current_char;

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.)

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:

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'.

  • Use initializer expressions. This means using an initializer list:

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

    Note that if you're initializing members with a construction parameter of the same name, you can use shorthand:

    Lexer(this._text)
      : _pos = -1,
        _current_char = '' {
      advance();
    }
    
  • Adding field initializers. This means initializing members inline in the class declaration.

    class Lexer {
      String _text = '';
      int _pos = -1,
      String _current_char = '';
    
  • 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.

You also might want to read: Dart assigning to variable right away or in constructor?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...