Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IsEmpty check returning wrong value for TextEditingController

The Problem

The isEmpty check is returning true for a non-empty String field. My guess is – TextEditingController’s String metadata is not updating with string update from an async load.

  1. I’m not sure if I’m missing something obvious or if this is a Dart/Flutter bug.
  2. Is there an idiomatic way to accomplish this check?

Here’s my code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  var textController = TextEditingController();

  @override
  void initState() {
    super.initState();
    init();
  }

  void init() async {
    final foo = await FooProvider.instance.getFoo(widget.uuid);
    textController.text = foo.title;
  }

The actual usage of the textController,


GestureDetector(
                onTap: textController.text.isEmpty
                    ? () {
                        debugPrint('Empty - ${textController.text}');
                      }
                    : () {
    // rest of the code

Output
(With actual field value "abc")
enter image description here

>Solution :

An alternative solution is to do the check inside a single implementation of onTap instead of two different ones, like this

GestureDetector(
            onTap: () {
                    if (textController.text.isEmpty) {
                      debugPrint('Empty - ${textController.text}');
                    } else {
                      // rest of the code
                    }
                  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading