Skip to content

form reset() function gives null pointer for a dropdownmenu with onChange=null #146335

Closed

Description

@JarnoD81

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.19.3, on macOS 14.2.1 23C71 darwin-x64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] IntelliJ IDEA Ultimate Edition (version 2023.3.2)
[✓] Connected device (3 available)
[✓] Network resources

We have a DropdownButtonFormField which we have disabled by setting the onChanged to null. This works fine as the look and feel of the widget show a disabled state and the user cannot change the value. I just figured out that we can also set the items to null and use the disabledHint property so I am not sure if setting onChanged to null is a "valid" way of disabling a DropdownButtonFormField.

Once the user is done submitting the form we reset it using a GlobalKey

final _formKey = GlobalKey(debugLabel: '_ClientEditState');

and call the reset() function on the formState like so:

_formKey.currentState?.reset()

This triggers a reset() on all form fields but on a DropdownButtonFormField without a onChanged it creates a NPE.

This is because of the following line of code in the dropdown reset() function:
_dropdownButtonFormField.onChanged!(value);

As onChanged can be null (and it seems also that this can be intended behaviour to disable the widget because the widget becomes disabled) there should be a null check around this call.

Activity

  1. added
    in triagePresently being triaged by the triage team
    on Apr 5, 2024
  2. self-assigned this
    on Apr 5, 2024
  3. bleroux commented on Apr 5, 2024

    @bleroux
    Contributor

    Thanks for the report.

    Code sample for repro
    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Example(),
        );
      }
    }
    
    class Example extends StatelessWidget {
      final _formKey = GlobalKey<FormState>();
      Example({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Form(
            key: _formKey,
            child: Column(
              children: [
                DropdownButtonFormField(
                  items: const [
                    DropdownMenuItem(
                      value: "item1",
                      child: Text("item1"),
                    )
                  ],
                  onChanged: null,
                ),
                ElevatedButton(
                  onPressed: () {
                    _formKey.currentState?.reset();
                  },
                  child: const Text("reset"),
                ),
              ],
            ),
          ),
        );
      }
    }
    
  4. added
    a: text inputEntering text in a text field or keyboard related problems
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on Apr 5, 2024
  5. moved this from To do to PR submitted in Nevercodeon Apr 5, 2024
  6. moved this from PR submitted to Done (PR merged) in Nevercodeon Apr 8, 2024
  7. added
    r: fixedIssue is closed as already fixed in a newer version
    on Apr 8, 2024
  8. github-actions commented on Apr 22, 2024

    @github-actions

    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.

  9. locked as resolved and limited conversation to collaborators on Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a: text inputEntering text in a text field or keyboard related problemsfound in release: 3.19Found to occur in 3.19found in release: 3.22Found to occur in 3.22frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-text-inputOwned by Text Input team

Type

No type

Projects

  • Status
    Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions