Repository navigation
TextEditingController strange behavior on the listener #64143
Copy link
Copy link
Open
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsfound in release: 3.3Found to occur in 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7Found to occur in 3.7has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packagespackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.team-text-inputOwned by Text Input teamOwned by Text Input teamtriaged-text-inputTriaged by Text Input teamTriaged by Text Input team
Description
Hi,
since the last stable 1.20.2 release, the listener of the TextEditingController has a strange behavior.
- on the first input, the listener is called twice
- when the controller is cleared, the listener is called 3 times.
- one with the empty text
- one with the previous text
- the last with the empty text again.
Steps to Reproduce
Code :
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController controller = TextEditingController();
@override
void initState() {
super.initState();
controller.addListener(() {
print(controller);
if (controller.selection.baseOffset > 0) {}
if (controller.text.length == 3) {
controller.clear();
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
controller: controller,
decoration: InputDecoration(hintText: 'input'),
),
],
),
),
);
}
}Expected results on 1.17.5
I/flutter ( 3162): TextEditingController#9066a(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: 0, extentOffset: 0, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter ( 3162): TextEditingController#9066a(TextEditingValue(text: ┤0├, selection: TextSelection(baseOffset: 1, extentOffset: 1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter ( 3162): TextEditingController#9066a(TextEditingValue(text: ┤00├, selection: TextSelection(baseOffset: 2, extentOffset: 2, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter ( 3162): TextEditingController#9066a(TextEditingValue(text: ┤000├, selection: TextSelection(baseOffset: 3, extentOffset: 3, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter ( 3162): TextEditingController#9066a(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
D/dalvikvm( 3162): GC_CONCURRENT freed 352K, 14% free 3264K/3780K, paused 2ms+0ms, total 4ms
Actual results on 1.20.2
I/flutter (25307): TextEditingController#83c21(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: 0, extentOffset: 0, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter (25307): TextEditingController#83c21(TextEditingValue(text: ┤0├, selection: TextSelection(baseOffset: 1, extentOffset: 1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: 0, end: 1)))
I/flutter (25307): TextEditingController#83c21(TextEditingValue(text: ┤0├, selection: TextSelection(baseOffset: 1, extentOffset: 1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter (25307): TextEditingController#83c21(TextEditingValue(text: ┤00├, selection: TextSelection(baseOffset: 2, extentOffset: 2, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter (25307): TextEditingController#83c21(TextEditingValue(text: ┤000├, selection: TextSelection(baseOffset: 3, extentOffset: 3, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter (25307): TextEditingController#83c21(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter (25307): TextEditingController#83c21(TextEditingValue(text: ┤000├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
I/flutter (25307): TextEditingController#83c21(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsfound in release: 3.3Found to occur in 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7Found to occur in 3.7has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesmaterial_ui package in flutter/packagespackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.team-text-inputOwned by Text Input teamOwned by Text Input teamtriaged-text-inputTriaged by Text Input teamTriaged by Text Input team
Activity
pedromassangocode commentedon Aug 19, 2020
I was able to reproduce part of this issue on latest master channel.
I can't reproduce this on
masterchannel. What is happening is that the first print happens when the TextFormField gains focus.This may be eventually caused because you're clearing the field inside the listener. I added a delay in the clear operation with a duration of one second and here is my output (the first line is when the field gains focus):
Note that after cleaning the field I got two prints, this may be the issue: the listener is being called multiple times when we call
controller.clear().complete sample code (with a delay before cleaning the text field)
flutter docctor -v
maheshj01 commentedon Jan 3, 2023
Reproducible on stable 3.3 and master 3.7,
Listener called on
console
flutter doctor -v (mac)
kamami commentedon May 24, 2023
Still happening in 3.10...
LuodiJackShen commentedon Dec 1, 2025
Still happening in 3.27.4