Home > database >  There isn’t a setter named 'selection' in class 'TextEditingController'
There isn’t a setter named 'selection' in class 'TextEditingController'

Time:08-17

I'm try to keep the cursor on the right of the TextField input using the 'controller.selection' line with 'onChange' as shown below; but getting an error

onChanged: (text) {
_myController.text = text;
_myController.selection = TextSelection.fromPosition(TextPosition(offset: 
_myController.text.length));
},     

the error:

There isn’t a setter named 'selection' in class 'TextEditingController'.
Try correcting the name to reference an existing setter, or declare the setter.

I have used this method before with no issues but for some reason getting this now.

CodePudding user response:

If you want to set both text and selection, you should set the value instead:

final selection = TextSelection.fromPosition(TextPosition(offset: text.length));

_myController.value = TextEditingValue(text, selection);

CodePudding user response:

It has bee solved by force upgrading the flutter sdk using 'flutter upgrade --force' - before that it was not upgrading for some reason.

Probably I modified some system file or library that I was not supposed to modify, but now everything seems to be fine back to normal.

  • Related