Skip to content

DropdownButtonFormField is not re-rendered after value is changed programmatically #56898

@lazarvgd

Description

@lazarvgd

I just created a simple app in order to see what am I doing wrong

The code:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  List<int> myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  int currentNumber = 1;
  int anotherNumber = 9;

  void _incrementCounter() {
    setState(() {
      _counter++;
      anotherNumber = _counter;
      currentNumber = _counter;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            DropdownButtonFormField<int>(
              value: currentNumber,
              items: myList.map((int value) {
                return DropdownMenuItem<int>(
                  value: value,
                  child: Container(
                    width: 200,
                    child: Text(
                      value.toString(),
                    ),
                  ),
                );
              }).toList(),
              onChanged: (int newValue) {
                setState(() {
                  anotherNumber = newValue;
                  currentNumber = newValue;
                });
              },
            ),
            DropdownButtonFormField<int>(
              value: anotherNumber,
              items: myList.map((int value) {
                return DropdownMenuItem<int>(
                  value: value,
                  child: Container(
                    width: 200,
                    child: Text(
                      value.toString(),
                    ),
                  ),
                );
              }).toList(),
              onChanged: (int newValue) {
                setState(() {
                  anotherNumber = newValue;
                });
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

The flutter doctor -v:

$ flutter doctor -v
[✓] Flutter (Channel stable, v1.17.0, on Mac OS X 10.15.4 19E287, locale en-RS)
    • Flutter version 1.17.0 at /Users/lazarjovicic/flutter
    • Framework revision e6b34c2b5c (9 days ago), 2020-05-02 11:39:18 -0700
    • Engine revision 540786dd51
    • Dart version 2.8.1

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/lazarjovicic/Library/Android/sdk
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.4.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.4.1, Build version 11E503a
    • CocoaPods version 1.8.4

[✓] Android Studio (version 3.6)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 45.1.1
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] VS Code (version 1.44.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.10.1

[✓] Connected device (1 available)
    • SM G970F • RF8M728WTMP • android-arm64 • Android 10 (API 29)

• No issues found!

The demonstration with code above:
https://streamable.com/fwwd46
EDIT:
The Expectation:
Update the first dropdown filed -> the second dropdown field will be updated automatically

The Result:
Update the first dropdown field -> the second dropdown field remains unchanged.

Please let me know if you need additional information or any further explanation regarding the problem.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: regressionIt was better in the past than it is nowf: material designflutter/packages/flutter/material repository.found in release: 1.17Found to occur in 1.17found in release: 1.18Occurs in 1.18frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work on

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions