[bug]: Setting autoFocus: true causes error
Describe the bug
I have a form with a TextFormField. When a user lands on the page that includes the form, I want the TextFormField to be active, so I set autoFocus: true. When I do that, it throws the following errors:
- TextEditingController was used after being disposed
- Another exception was thrown: Duplicate GlobalKey detected in widget tree
Has anyone encountered this issue? I have several other forms within my app and this is true for all of them. I have followed the excellent documentation here, so the code is boilerplate. The forms work fine when autoFocus: false.
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
The following assertion was thrown building RawGestureDetector(state:
RawGestureDetectorState#955ba(gestures: [tap, long press, tap and pan], excludeFromSemantics: true,
behavior: translucent)):
A TextEditingController was used after being disposed.
Once you have called dispose() on a TextEditingController, it can no longer be used.
The relevant error-causing widget was:
TextFormField
TextFormField:file:///Users/pbergmans/Code/my_project/lib/ui/views/assistant/assistant_form.dart:34:13
When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 296:3 throw_
packages/flutter/src/foundation/change_notifier.dart 179:9 <fn>
packages/flutter/src/foundation/change_notifier.dart 185:14 debugAssertNotDisposed
packages/flutter/src/foundation/change_notifier.dart 271:27 addListener
packages/flutter/src/foundation/change_notifier.dart 499:14 addListener
packages/flutter/src/widgets/transitions.dart 118:14 didUpdateWidget
packages/flutter/src/widgets/framework.dart 5659:55 update
packages/flutter/src/widgets/framework.dart 3827:14 updateChild
packages/flutter/src/widgets/framework.dart 6776:14 update
packages/flutter/src/widgets/framework.dart 3827:14 updateChild
packages/flutter/src/widgets/framework.dart 5512:16 performRebuild
packages/flutter/src/widgets/framework.dart 5650:11 performRebuild
packages/flutter/src/widgets/framework.dart 5203:7 rebuild
packages/flutter/src/widgets/framework.dart 5673:5 update
packages/flutter/src/widgets/framework.dart 3827:14 updateChild
packages/flutter/src/widgets/framework.dart 5512:16 performRebuild
packages/flutter/src/widgets/framework.dart 5650:11 performRebuild
packages/flutter/src/widgets/framework.dart 5203:7 rebuild
packages/flutter/src/widgets/framework.dart 5673:5 update
packages/flutter/src/widgets/framework.dart 3827:14 updateChild
packages/flutter/src/widgets/framework.dart 6776:14 update
packages/flutter/src/widgets/framework.dart 3827:14 updateChild
packages/flutter/src/widgets/framework.dart 5512:16 performRebuild
packages/flutter/src/widgets/framework.dart 5650:11 performRebuild
packages/flutter/src/widgets/framework.dart 5203:7 rebuild
packages/flutter/src/widgets/framework.dart 5673:5 update
packages/flutter/src/widgets/framework.dart 3827:14 updateChild
packages/flutter/src/widgets/framework.dart 6776:14 update
packages/flutter/src/widgets/framework.dart 3827:14 updateChild
packages/flutter/src/widgets/framework.dart 6776:14 update
packages/flutter/src/widgets/framework.dart 3827:14 updateChild
packages/flutter/src/widgets/framework.dart 6776:14 update
packages/flutter/src/widgets/framework.dart 3827:14 updateChild
packages/flutter/src/widgets/framework.dart 5512:16 performRebuild
packages/flutter/src/widgets/framework.dart 5650:11 performRebuild
packages/flutter/src/widgets/framework.dart 5203:7 rebuild
packages/flutter/src/widgets/framework.dart 2905:18 buildScope
packages/flutter/src/widgets/binding.dart 1136:9 drawFrame
packages/flutter/src/rendering/binding.dart 443:5 [_handlePersistentFrameCallback]
packages/flutter/src/scheduler/binding.dart 1392:7 [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1313:9 handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1171:5 [_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1404:5 invoke
lib/_engine/engine/platform_dispatcher.dart 307:5 invokeOnDrawFrame
lib/_engine/engine/initialization.dart 187:36 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 426:37 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 431:39 dcall
══════════════════════════════════════════
2 Another exception was thrown: Assertion failed: file:///Users/pbergmans/Code/flutter/packages/flutter/lib/src/rendering/object.dart:3337:12
💡 ScaffoldViewModel | isUserSignedIn - yes
💡 MqttService | connect - trying...
Another exception was thrown: Duplicate GlobalKey detected in widget tree.
What operating system do you use?
macOS
Information about the installed tooling
No response
Steps to reproduce the issue
Create a form and set autoFocus: true
Expected behavior
I’d expect no errors.
Screenshots
No response
Additional Context
No response
@pieterbergmans this is interesting.
I assume the autofocus is set on the InputTextField widget.
What's probably happening is that the auto focus is causing the bound value to be updated which is causing a rebuild, most likely during contruction of the ViewModel or while the widget tree is still being built.
IS there anything else in your code that causes a rebuild during that time?
Please post your form code and the viewModel code here.
@FilledStacks he is using the boiler plate code on docs