I'm trying to pass data from TextField with onChange property to onPressed button.
If I type my string as below:
String newTextTitle;
then I get error on print(newTextTitle);:
The non-nullable local variable 'newTextTitle' must be assigned before it can be used. Try giving it an initializer expression, or ensure that it's assigned on every execution path.
So I change it to
String? newTextTitle;
then the error won't appear again. But The data won't pass from TextField to my button, actually passing null.
And if I assigned some string then it is printing always what I assigned regardless of any change in the TextField.
My TextField code:
TextField(
autofocus: true,
textAlign: TextAlign.center,
onChanged: (newValue) {
newTextTitle = newValue;
},
),
My button code:
TextButton(
onPressed: () {
print('Passing Test $newTextTitle');
},
),
my output consol:
I/flutter (23788): Passing Test null
This code is worked so fine in older flutter.
But now I used Flutter 2.5.2 and there is somthing has been changed.
CodePudding user response:
Using a TextEditingController is the Flutter recommended way of doing what your trying to do. See (
String? newTextTitle; //past it here
Buildwidget(){
String? newTextTitle; //remove from here
}
