I'm building a small class just to train/learn Dart and I'm getting this error:
Error: Can't access 'this' in a field initializer to read 'firstName'.
String completeName = "${firstName} ${lastName}";
^^^^^^^^^
lib/main.dart:13:41:
Error: Can't access 'this' in a field initializer to read 'lastName'.
String completeName = "${firstName} ${lastName}";
^^^^^^^^
Error: Compilation failed.
Here is my code:
void main()
{
var user = new User();
user.firstName = "John";
user.lastName = "Smith";
user.printName();
}
class User
{
String firstName;
String lastName;
String completeName = "$firstName $lastName";
void printName()
{
print(completeName);
}
}
My programming background is in C/C++ and Python and I never had a similar problem. Can anyone help me to understand why I'm getting this error? Thanks in advance!