sudo
sudo

Reputation: 353

The this keyword in Dart, string interpolation

The Dart Editor doesn't seem to recognize $this.keyword, where keyword is a field of a class. The expression is located in a function of a method context. But it does recognize $keyword. The confusion is, why doesn't it recognize $this.keyword, the this keyword should be rigid like in C# or Java.

class TryMe {
 String keyword;

 void hi(Function callback) {
  callback(() => return '$this.keyword');
 }
}

Upvotes: 3

Views: 1003

Answers (2)

Shailen Tuli
Shailen Tuli

Reputation: 14171

I'm not sure what your code is doing, but you don't usually use this to refer to class fields. You can rewrite your code as:

class TryMe {
  String keyword;

  void hi(Function callback) {
    callback(() => keyword);
  }
}

Also note that you don't need a return when using => syntax.

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657546

If the interpolation part is not an identifier but an expression you need to add {}

callback(() => return '${this.keyword}');

Upvotes: 7

Related Questions