Home > Net >  Dart object as a value
Dart object as a value

Time:11-03

I remember that Dart objects have a method which does an object return a value by default without pointing to an property. Example:

class A {
  final String name;
  A(this.name);
  ...
}

main() {
  var obj = A('chesu');
  print(obj   ' locuaz');
}

Output: chesu locuaz

But I don't remember that method or decorator and it is not toString().

CodePudding user response:

Use interpolation to compose strings and values. It will automatically call toString method of value.

class A {
  A(this.name);
  final String name;
  
  @override
  toString() => name;
}

main() {
  var obj = A('chesu');
  print('$obj locuaz');
}

CodePudding user response:

I finally found what I was looking for, it's called callable classes.

  • Related