You are just not setting state synchronously that’s all. What onChanged
does is exactly possible with this approach:
class _TestFormState extends State<TestForm> {
late TextEditingController controller;
@override
void initState() {
controller = TextEditingController()
..addListener(() {
setState(() {});
});
super.initState();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Text('Current Value: ${controller.text}'),
TextField(
controller: controller,
),
],
);
}
}
As you see, we have listener that setting state every time state of the controller changes. This is exactly what onChanged
does.
So, about benefits, you can achieve everything with both approach, it’s a subjective way.
About benefits:
If you need to hold field values within Stream
, onChanged
is what you need. In other cases you may use controller
.
Actually you won’t need both in most of time in my opinion because TextFormField + Form
within StatefulWidget
is quite complete way to implement form pages. Checkout cookbook: https://flutter.dev/docs/cookbook/forms/validation