Home > other >  The instance member 'myString' can't be accessed in an initializer. TextEditingContro
The instance member 'myString' can't be accessed in an initializer. TextEditingContro

Time:10-21

String firstName = ''; 

TextEditingController fnameEditController = TextEditingController(text:firstName);

I get the error when trying to add a string to TextEditingController(text:firstName) the error:

The instance member 'firstName' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expressiondartimplicit_this_reference_in_initializer
String firstName 

CodePudding user response:

Of course that can not work..

Try doing it in initState like this

String firstName = '';
late TextEditingController _controller;

@override
initState() {
    // this will work
    _controller = TextEditingController(text: firstName);
}

CodePudding user response:

Well you can't because this 'firstName' is not a constant variable. You can assign it by making constant variable like

  static const String firstName = '';

  TextEditingController fnameEditController =
      TextEditingController(text: firstName);

Also, as @emanuel sanga answered, you can use initState

For more

  • Related