Skip to content

TextField with lots of text can hang app #90063

Open

Description

@dnfield

For example, this app:

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  final TextEditingController controller = TextEditingController();
  runApp(
    MaterialApp(
      home: Material(
        child: Center(
          child: Column(
            children: [
              TextField(controller: controller),
              TextButton(
                onPressed: () {
                  controller.text = 'abcdef' * 100000;
                },
                child: Text('Fill'),
              ),
            ],
          ),
        ),
      ),
    ),
  );
}

on my MacBook pro causes the app to freeze after pressing fill. I discovered this when accidentally pasting a very large amount of text into a text field while testing something else. In that case, the text field probably also had some extra styling and input formatters, and the app completely hung (and hot restarting caused it to crash). This sample app does not crash on hot restart, but I suspect adding formatters or making the text even larger might cause it to.

Activity

  1. added
    a: text input
    framework
    c: performance
    a: desktop
    perf: speed
    on Sep 14, 2021
  2. added
    P1
    on Sep 14, 2021
  3. goderbauer commented on Sep 15, 2021

    @goderbauer
    Member
  4. maheshj01 commented on Sep 22, 2021

    @maheshj01
    Member

    I am able to reproduce the issue on m1 mac the app is almost frozen, verified on stable 2.5.1 and master 2.6.0-6.0.pre.171. I could not reproduce the crash (on hot restart) after adding style.

    code sample
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({Key? key, required this.title}) : super(key: key);
    
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      final TextEditingController controller = TextEditingController();
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
                TextField(
                  controller: controller,
                  style: TextStyle(
                      fontSize: 20,
                      color: Colors.red,
                      letterSpacing: 2,
                      fontStyle: FontStyle.italic),
                  maxLines: 5,
                ),
                TextButton(
                  onPressed: () {
                    controller.text = 'abcdef' * 100000;
                  },
                  child: Text('Fill'),
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ),
        );
      }
    }
    
    flutter doctor -v
    [✓] Flutter (Channel stable, 2.5.1, on macOS 11.6 20G165 darwin-arm, locale en-GB)
        • Flutter version 2.5.1 at /Users/mahesh/Documents/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision ffb2ecea52 (5 days ago), 2021-09-17 15:26:33 -0400
        • Engine revision b3af521a05
        • Dart version 2.14.2
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-31, build-tools 31.0.0
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.5.1, Build version 12E507
        • CocoaPods version 1.10.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.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.8+10-b944.6916264)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 60.1.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.59.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.26.0
    
    [✓] Connected device (2 available)
        • macOS (desktop) • macos  • darwin-arm64   • macOS 11.6 20G165 darwin-arm
        • Chrome (web)    • chrome • web-javascript • Google Chrome 94.0.4606.54
    
    • No issues found!
    
    [✓] Flutter (Channel master, 2.6.0-6.0.pre.171, on macOS 11.6 20G165 darwin-arm, locale en-GB)
        • Flutter version 2.6.0-6.0.pre.171 at /Users/mahesh/Documents/flutter_master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 321c872ca7 (6 hours ago), 2021-09-22 02:38:06 -0400
        • Engine revision b0f3c0f7e4
        • Dart version 2.15.0 (build 2.15.0-127.0.dev)
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-31, build-tools 31.0.0
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 12.5.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • CocoaPods version 1.10.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 4.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.8+10-b944.6916264)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 60.1.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.59.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.26.0
    
    [✓] Connected device (2 available)
        • macOS (desktop) • macos  • darwin-arm64   • macOS 11.6 20G165 darwin-arm
        • Chrome (web)    • chrome • web-javascript • Google Chrome 94.0.4606.54
    
    • No issues found!
    

    on a side note: adding maxlines to textfield it takes much longer (around 10s or more) to fill text into textfied.

  5. added
    found in release: 2.5
    found in release: 2.6
    on Sep 22, 2021
  6. justinmc commented on Sep 23, 2021

    @justinmc
    Contributor

    Thanks for the reproducible issue, this has been in the back of my mind for awhile. I want to figure out exactly what the bottleneck is here. Just some quick thoughts for now:

    • This isn't really an insurmountable amount of text (~800kb back of the napkin?).
    • Currently, the entire contents of the field is synced between the framework and engine on every change, which is going to be a performance concern even if it's not exactly the cause of this slowdown here.
    • @Renzo-Olivares introduced the TextEditingDeltas API in Framework can receive TextEditingDeltas from engine #88477, which may give us a way to work with deltas instead of the entire string.
  7. added
    has reproducible steps
    on Sep 29, 2021
  8. cbracken commented on Oct 18, 2021

    @cbracken
    Member
  9. charmoan commented on Oct 22, 2021

    @charmoan
  10. charmoan commented on Oct 22, 2021

    @charmoan
  11. 33 remaining items

  12. added
    P2
    triaged-framework
    on Jul 25, 2023
  13. Justus-M commented on Dec 2, 2023

    @Justus-M

    Any updates or workarounds?

  14. Srimathi622 commented on Dec 7, 2023

    @Srimathi622

    @cbracken For all desktop platforms deltas are currently supported but the TextField has not been migrated over to use it, you would have to create your own DeltaTextInputClient to use it.

    We have documentation on how to enable it here https://api.flutter.dev/flutter/services/DeltaTextInputClient-class.html . Though we don't have a concrete guide on how to implement your own TextInputClient/DeltaTextInputClient.

    We currently do not support sending deltas to the platforms text input plugin through TextInput.setEditingState, but this is something we can definitely consider if it will help performance.

    is there any document available for creating our own TextField using DeltaTextInputClient? I am facing a performance issue. The speed is reduced when we have a huge record

  15. MegatronKing commented on Dec 7, 2023

    @MegatronKing

    In my opinion, TextField is suitable for small input text, such as account, password. For the large text, a specific Widget is required.

    I wrote a code editor for the Reqable project, which basically solved this issue, I can provide some ideas.

    • Split text by lines.
    • UseSingleChildRenderObjectWidget to take over layout and drawing, rather than TextField.
    • Don't layout and draw all the content, just deal with the visible part of the screen.
    • When editing, only the line where the cursor is located is sent to the IME.
    video.mp4

    However, there is still a problem, when the single line of text is too large, the layout will cause lag. I tested VS Code and there was no such issue, this seems to be a performance issue from graphics engine.

  16. dtruong27 commented on Dec 7, 2023

    @dtruong27

    We had a workaround but only for the Web platform. We wanted to use a TextAreaElement but there is a bug that onKeyUp and onKeyDown events are not triggered. We then ended up using IFrameElement with <textarea> in the srcdoc. Our workaround looks like this:

      html.HtmlElement longtextForm(int id, double height) {
        final iframe = html.IFrameElement()
          ..style.cssText =
              "height:100%;width:100%;scrolling:no;border:1px solid black;background:#FFFFFF;";
    
        iframe.srcdoc = '''
    <script>
      const changeText = function (e) {
        const v = document.getElementById("text-editor").value;
        window.parent.postMessage(v, "*");
      };
    </script>
    <textarea id="text-editor" style="width:100%;height:100%;min-height:${(height * 0.95).round()}px;
    overflow:auto;font-size:11pt;font-weight:300;font-family:Helvetica,sans-serif;resize:none;border:none;outline:none;background:none;margin:0px;box-sizing:border-box;" 
    onkeyup=changeText()>
    $content
    </textarea>
    ''';
    
        final div = html.DivElement()
          ..style.width = "100%"
          ..style.height = "100%"
          ..children = [iframe];
    
        return div;
      }
    
      void observeEvents() {
        html.window.onMessage.listen((event) {
          final text = event.data;
          if (text != null && text is String) {
            onContentUpdated(text);
          }
        });
      }
    
  17. Srimathi622 commented on Dec 7, 2023

    @Srimathi622

    Thanks for your replies. Is there any workaround for the desktop application (Windows and macOS)? I need a code editor to handle huge text.

  18. MegatronKing commented on Feb 5, 2024

    @MegatronKing

    Thanks for your replies. Is there any workaround for the desktop application (Windows and macOS)? I need a code editor to handle huge text.

    Try this one: https://pub.dev/packages/re_editor

  19. Srimathi622 commented on Mar 12, 2024

    @Srimathi622

    Thanks a lot. It works

  20. Markaleth commented on May 5, 2024

    @Markaleth

    Just to add to the discussion., this issue also happens with plain Text or SelectableText widgets. Or at least, my assumption is that it's the same root cause at play.

    My current use case is trying to render a very large JSON response to the UI in a SelectableText widget with flutter web. The length of the string i'm passing to the SelectableText / Text widget is between 270k and 870k characters.

    This results in the website hanging for anywhere between 250 ms to close to 1000 ms.

  21. Maruf-Elsa commented on Dec 12, 2024

    @Maruf-Elsa

    I had the same issue when validating email. The reason was Flutter was not able to handle complex regex and was hanging the app when the email was more than 35 characters. I hope the Flutter team takes a note of this and fixes it.

    Complex regex hanging the app - r'^[_A-Za-z0-9-+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$'
    Simple working regex for email- r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'

  22. Srimathi622 commented on Feb 26, 2026

    @Srimathi622

    @Srimathi622 Have a look at this one: https://github.com/heckmon/code_forge

    Sure, Heckmon, I will do. It seems interesting. Thanks for sharing it

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

    P2a: text inputc: performancefound in release: 2.5found in release: 2.6frameworkhas reproducible stepsperf: speedteam-frameworktriaged-framework

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions