Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextFormField Android "Failed assertion: 'initialValue == null || controller == null': is not true." Despite controller being null in debug. #18044

Closed
msmiech opened this issue May 30, 2018 · 15 comments
Labels
c: crash Stack traces logged to the console f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Comments

@msmiech
Copy link

msmiech commented May 30, 2018

I want to try out flutter for a new project (Android mobile app primarily) but I regularly encounter the following error at building a TextFormField during debug on my device (Samsung Galaxy S7 G930F, Android 7.0):

Syncing files to device SM G930F...
I/flutter (32149): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (32149): The following assertion was thrown building LoginPage(dirty, state: _LoginPageState#7e27e):
I/flutter (32149): 'package:flutter/src/material/text_form_field.dart': Failed assertion: line 72 pos 15: 'initialValue
I/flutter (32149): == null || controller == null': is not true.
I/flutter (32149): 
I/flutter (32149): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (32149): more information in this error message to help you determine and fix the underlying cause.

The project template that I have used for trying this out is https://github.com/putraxor/flutter-login-ui

When debugging the affected sections (i.e., assert statements) - assert(initialValue == null || controller == null); the debugger says initialValue is a valid string, as defined, and the controller is null (which I have explicitly set to null before and asserted that before building the TextFormField). This issue occurs primarily during debug and results in a red screen on the device. It does not always appear in debug mode, after some manual restarts (without refreshing the code), it works sometimes beyond this point, sometimes not. This issue does not appear when running the app in regular (non-debug) mode. This issue does also not appear when using an Android emulator (e.g. Android P x86 image).

Doctor summary:
[√] Flutter (Channel beta, v0.4.4, on Microsoft Windows [Version 10.0.17134.81], locale de-AT)
    • Flutter version 0.4.4 at C:\work\flutter
    • Framework revision f9bb4289e9 (3 weeks ago), 2018-05-11 21:44:54 -0700
    • Engine revision 06afdfe54e
    • Dart version 2.0.0-dev.54.0.flutter-46ab040e58

[√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    • Android SDK at C:\work\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-P, build-tools 27.0.3
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)
    • All Android licenses accepted.

[√] Android Studio (version 3.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 24.2.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b02)

[√] IntelliJ IDEA Ultimate Edition (version 2018.1)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2018.1
    • Flutter plugin version 24.2.2
    • Dart plugin version 181.4892.1

[√] VS Code, 64-bit edition (version 1.23.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Dart Code extension version 2.12.1

[√] Connected devices (1 available)
    • SM G930F • ad0617020e32eacbe5 • android-arm64 • Android 7.0 (API 24)

• No issues found!

screenshot_20180530-230636

@HansMuller HansMuller added the f: material design flutter/packages/flutter/material repository. label May 31, 2018
@HansMuller
Copy link
Contributor

I wasn't able to reproduce this failure mode.

I put a one-file version of the app (sans assets) here: https://gist.github.com/HansMuller/266cf806708fffef5440860c3892e1ed

If you could explain what additional changes and what sequence of reloads/restarts are needed to trigger this failure, I'd appreciate it

@msmiech
Copy link
Author

msmiech commented Jun 1, 2018

Thank you for looking into it.
I created a new project with a different name using the same code and the error does not appear on the device anymore... I apologize for raising this issue perhaps too early.
For a brief amount of time there was in fact a problem with the TextFormField parameters but it appears that any newer version of the APK, where this issue was resolved, was not installed/updated properly on my device (maybe a caching problem or a problem of the device itself, I unfortunately cannot tell). Is there a recommended way to force a clean reinstall of the app? (Simply uninstalling it on the device and then reinstalling it via Android Studio did not change anything within the "broken" project)

@HansMuller
Copy link
Contributor

Sometimes, it helps to rm -rf build in the project's directory, before flutter run, particularly if you've just updated the packages flutter depends on with flutter update-packages

@zoechi zoechi added the framework flutter/packages/flutter repository. See also f: labels. label Jul 22, 2018
@brmc
Copy link

brmc commented Aug 12, 2018

I just had this problem. For anyone else coming along, check if you have passed both initialValue and controller to the TextField. If so, pass text to the TextEditingController and remove the initialValue from the TextField:

Instead of this:

TextFormField(
  initialValue: 'foo',
  controller: TextEditingController(),
), 

do this:

TextFormField(
  controller: TextEditingController(text: 'foo'),
), 

My problem arose when I used a refactoring option in Android Studio to convert from a stateless to a stateful widget

@zoechi
Copy link
Contributor

zoechi commented Aug 23, 2018

@mart1n8891 Is this still an issue?

@PaoloGi
Copy link

PaoloGi commented Oct 19, 2018

still an issue there

@JotaFerreira
Copy link

same problem

@acheronian
Copy link

+1

@zoechi zoechi added this to the Goals milestone Nov 14, 2018
@zoechi zoechi added the c: crash Stack traces logged to the console label Nov 14, 2018
@HansMuller
Copy link
Contributor

HansMuller commented Nov 14, 2018

This isn't a bug. The failed assertion indicates that you must either provide a controller or an initialValue, but not both:

I/flutter (16012): 'package:flutter/src/material/text_form_field.dart': Failed assertion: line 101 pos 15:
I/flutter (16012): 'initialValue == null || controller == null': is not true.

Both properties specify the text field's initial value. If the controller is specified it's typically a Stateful widget's State field so that rebuilding the stateful parent widget doesn't reset the text field's value. If both values were specified and the implementation used the initialValue to set the controller's value, then rebuilding the stateful parent widget would similarly reset the textfield's values.

There's more about this in the PR that introduced the assertion: #15484

@HansMuller
Copy link
Contributor

Note also: if there's a test case out there where only specifying an initial value inexplicably triggers the assertion (as was originally reported), please reopen this bug.

TextFormField(
  initialValue: 'foo',
  controller: null,
)

@J1GGZ
Copy link

J1GGZ commented Apr 30, 2019

I'm experiencing the same issue currently. I am primarily looking to have a user enter his/her location coordinates for a map marker. I was hoping to lazy load it but what about having the textfield filled upon a button press?

@anisbouziane
Copy link

This isn't a bug. The failed assertion indicates that you must either provide a controller or an initialValue, but not both:

I/flutter (16012): 'package:flutter/src/material/text_form_field.dart': Failed assertion: line 101 pos 15:
I/flutter (16012): 'initialValue == null || controller == null': is not true.

Both properties specify the text field's initial value. If the controller is specified it's typically a Stateful widget's State field so that rebuilding the stateful parent widget doesn't reset the text field's value. If both values were specified and the implementation used the initialValue to set the controller's value, then rebuilding the stateful parent widget would similarly reset the textfield's values.

There's more about this in the PR that introduced the assertion: #15484

nice one bro, it's solved for me !!

@MaxanDsouza
Copy link

This isn't a bug. The failed assertion indicates that you must either provide a controller or an initialValue, but not both:

I/flutter (16012): 'package:flutter/src/material/text_form_field.dart': Failed assertion: line 101 pos 15:
I/flutter (16012): 'initialValue == null || controller == null': is not true.

Both properties specify the text field's initial value. If the controller is specified it's typically a Stateful widget's State field so that rebuilding the stateful parent widget doesn't reset the text field's value. If both values were specified and the implementation used the initialValue to set the controller's value, then rebuilding the stateful parent widget would similarly reset the textfield's values.

There's more about this in the PR that introduced the assertion: #15484

Thanks! This worked!

@romreed
Copy link

romreed commented Jul 10, 2020

I catch this error at
Flutter 1.17.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 8af6b2f (10 дней назад) • 2020-06-30 12:53:55 -0700
Engine • revision ee76268252
Tools • Dart 2.8.4

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: crash Stack traces logged to the console f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

No branches or pull requests