Skip to content

TextEditingController strange behavior on the listener #64143

Open

Description

@PsyOhm23

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)))

Activity

  1. pedromassangocode commented on Aug 19, 2020

    @pedromassangocode

    I was able to reproduce part of this issue on latest master channel.

    • on the first input, the listener is called twice

    I can't reproduce this on master channel. What is happening is that the first print happens when the TextFormField gains focus.

    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.

    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):

    flutter: TextEditingController#87c5c(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: 0, extentOffset: 0, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
    flutter: TextEditingController#87c5c(TextEditingValue(text: ┤q├, selection: TextSelection(baseOffset: 1, extentOffset: 1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
    flutter: TextEditingController#87c5c(TextEditingValue(text: ┤qw├, selection: TextSelection(baseOffset: 2, extentOffset: 2, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
    flutter: TextEditingController#87c5c(TextEditingValue(text: ┤qwe├, selection: TextSelection(baseOffset: 3, extentOffset: 3, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
    flutter: TextEditingController#87c5c(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
    flutter: TextEditingController#87c5c(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: 0, extentOffset: 0, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))
    

    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)
    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) {
            Future.delayed(Duration(seconds: 1), () {
              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'),
                ),
              ],
            ),
          ),
        );
      }
    }
    
    flutter docctor -v 
    [✓] Flutter (Channel master, 1.21.0-10.0.pre.138, on Mac OS X 10.15.6 19G2021,
        locale en)
        • Flutter version 1.21.0-10.0.pre.138 at
          /Users/pedromassango/dev/SDKs/flutter_master
        • Framework revision 3562b3c0de (27 hours ago), 2020-08-18 01:46:02 -0400
        • Engine revision 02de4eb7ce
        • Dart version 2.10.0 (build 2.10.0-37.0.dev)
    
     
    [!] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
        • Android SDK at /Users/pedromassango/Library/Android/sdk
        • Platform android-30, build-tools 30.0.1
        • Java binary at: /Applications/Android
          Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6222593)
        ! Some Android licenses not accepted.  To resolve this, run: flutter doctor
          --android-licenses
    
    [✓] Xcode - develop for iOS and macOS (Xcode 11.6)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 11.6, Build version 11E708
        • CocoaPods version 1.9.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [!] Android Studio (version 4.0)
        • Android Studio at /Applications/Android Studio.app/Contents
        ✗ Flutter plugin not installed; this adds Flutter specific functionality.
        ✗ Dart plugin not installed; this adds Dart specific functionality.
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6222593)
    
    [✓] IntelliJ IDEA Community Edition (version 2020.2)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 48.1.4
        • Dart plugin version 202.6397.47
    
    [!] VS Code (version 1.48.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        ✗ Flutter extension not installed; install from
          https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
    
    [✓] Connected device (4 available)
        • sdk gphone x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API
          30) (emulator)
        • macOS (desktop)         • macos         • darwin-x64     • Mac OS X
          10.15.6 19G2021
        • Web Server (web)        • web-server    • web-javascript • Flutter Tools
        • Chrome (web)            • chrome        • web-javascript • Google Chrome
          84.0.4147.135
    
    ! Doctor found issues in 3 categories.
    
  2. added
    a: text inputEntering text in a text field or keyboard related problems
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    on Aug 19, 2020
  3. maheshj01 commented on Jan 3, 2023

    @maheshj01
    Member

    Reproducible on stable 3.3 and master 3.7,

    Listener called on

    • tap (+1)
    • Text enter (+2 for two characters)
    • called twice on clear (+2)
    console
    Restarted application in 408ms.
    flutter: Listener called
    flutter: Listener called
    flutter: Listener called
    flutter: Listener called
    flutter: Listener called
    
    flutter doctor -v (mac)
    [✓] Flutter (Channel master, 3.7.0-13.0.pre.131, on macOS 13.1 22C65 darwin-arm64, locale en-IN)
        • Flutter version 3.7.0-13.0.pre.131 on channel master at /Users/mahesh/Development/flutter_master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 0196e6050b (2 days ago), 2022-12-31 11:10:23 -0500
        • Engine revision 932591ec04
        • Dart version 3.0.0 (build 3.0.0-76.0.dev)
        • DevTools version 2.20.0
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks
          and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0-rc4
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14A400
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.74.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.56.0
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
        • macOS (desktop)        • macos                                • darwin-arm64   • macOS 13.1 22C65 darwin-arm64
        • Chrome (web)           • chrome                               • web-javascript • Google Chrome 108.0.5359.124
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
        
    • No issues found!
    
    [✓] Flutter (Channel stable, 3.3.9, on macOS 12.6 21G115 darwin-arm, locale en-IN)
        • Flutter version 3.3.9 on channel stable at /Users/mahesh/Development/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision b8f7f1f986 (24 hours ago), 2022-11-23 06:43:51 +0900
        • Engine revision 8f2221fbef
        • Dart version 2.18.5
        • DevTools version 2.15.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0-rc4
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14A400
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.70.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.53.20221101
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            •
          com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
        • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm
        • Chrome (web)           • chrome                               • web-javascript • Google Chrome 107.0.5304.110
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    
  4. kamami commented on May 24, 2023

    @kamami

    Still happening in 3.10...

  5. added and removed
    team-designOwned by Design Languages team
    triaged-designTriaged by Design Languages team
    on Mar 4, 2025
  6. added
    P2Important issues not at the top of the work list
    on Mar 13, 2025
  7. LuodiJackShen commented on Dec 1, 2025

    @LuodiJackShen

    Still happening in 3.27.4

  8. added
    packageflutter/packages repository. See also p: labels.
    and removed
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsfound in release: 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagespackageflutter/packages repository. See also p: labels.team-text-inputOwned by Text Input teamtriaged-text-inputTriaged by Text Input team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions