Skip to content

[FormBuilderDropdown]: <If onChanged Function is added, the FormBuilderDropdown stop working> #1252

Closed
@CarmonaCarlos

Description

@CarmonaCarlos

Is there an existing issue for this?

  • I have searched the existing issues

Package/Plugin version

9.0.0

Flutter doctor

Flutter doctor

[√] Flutter (Channel stable, 3.10.0, on Microsoft Windows [Version 10.0.22621.1702], locale es-MX)
• Flutter version 3.10.0 on channel stable at C:\Users\Carlos\flutter_sdk\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 84a1e904f4 (6 days ago), 2023-05-09 07:41:44 -0700
• Engine revision d44b5a94c9
• Dart version 3.0.0
• DevTools version 2.23.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
• Android SDK at C:\Users\Carlos\AppData\Local\Android\sdk
• Platform android-33, build-tools 31.0.0
• Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)
• All Android licenses accepted.

[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.5.5)
• Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
• Visual Studio Community 2022 version 17.5.33627.172
• Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2022.2)
• Android Studio at C:\Program Files\Android\Android Studio
• 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 17.0.6+0-b2043.56-9586694)

[√] VS Code (version 1.78.2)
• VS Code at C:\Users\Carlos\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.64.0

[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.22621.1702]
• Chrome (web) • chrome • web-javascript • Google Chrome 113.0.5672.93
• Edge (web) • edge • web-javascript • Microsoft Edge 113.0.1774.42

[√] Network resources
• All expected network resources are available.

• No issues found!

Minimal code example

Code sample
import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:form_builder_validators/form_builder_validators.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter FormBuilder Demo',
      debugShowCheckedModeBanner: false,
      localizationsDelegates: [
        FormBuilderLocalizations.delegate,
        ...GlobalMaterialLocalizations.delegates,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: FormBuilderLocalizations.supportedLocales,
      home: Example(),
    );
  }
}

class Example extends StatefulWidget {
  const Example({Key? key}) : super(key: key);

  @override
  State<Example> createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  int? selectedLineId;
  final GlobalKey<FormBuilderState> _formKey = GlobalKey<FormBuilderState>();
  late List<Line> lines;

  @override
  void initState() {
    lines = _initLines();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Example')),
      body: FormBuilder(
        key: _formKey,
        child: Column(
          children: <Widget>[
            FormBuilderDropdown<int>(
              name: 'lineId',
              valueTransformer: (int? val) => val?.toInt(),
              decoration: const InputDecoration(
                  labelText: 'lines', icon: Icon(Icons.ad_units)),
              validator: FormBuilderValidators.compose(
                  [FormBuilderValidators.required()]),
              items: lines
                  .map((Line item) => DropdownMenuItem<int>(
                        value: item.id,
                        child: Text(item.name),
                      ))
                  .toList(),
              onChanged: (int? value) {
                setState(() {
                  selectedLineId = value;
                });
              },
            )
          ],
        ),
      ),
    );
  }

  List<Line> _initLines() {
    //This is just for test purpose
    return <Line>[
      Line(
        id: 1,
        name: 'Test',
      ),
      Line(
        id: 2,
        name: 'Test 2',
      )
    ];
  }
}

class Line {
  final int id;
  final String name;

  Line({required this.id, required this.name});
}

Current Behavior

If I add setState inside of onChanged the FormBuilderDropdown is not working correctly:

If I select an option:

01

Then nothing is selected:

02

On previous version it was working once I update to 9.0.0 it stop to works.

Expected Behavior

If I select an option

For example:

01

Then the option is selected:

03

Steps To Reproduce

  1. Add setState to onChanged

Aditional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions