Skip to content

Widget doesn't update when items change after valueListenable #392

@refi64

Description

@refi64

Sample reproducer (in a new flutter create project, with dropdown_button2: 3.0.0-beta.22):

import 'package:flutter/material.dart';
import 'package:dropdown_button2/dropdown_button2.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var selected = ValueNotifier<String?>(null);
  final allThings = ['abc', 'def'];
  List<String>? things;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          spacing: 32,
          children: <Widget>[
            DropdownButton2(
              isExpanded: true,
              valueListenable: selected,
              items: things != null
                  ? [
                      for (var thing in things!)
                        DropdownItem(value: thing, child: Text(thing)),
                    ]
                  : null,
            ),
            for (var thing in allThings)
              FilledButton(
                child: Text("Set valueListenable to '$thing'"),
                onPressed: () async {
                  selected.value = thing;
                },
              ),
            FilledButton(
              child: Text('Set items to $allThings'),
              onPressed: () {
                setState(() {
                  things = allThings;
                });
              },
            ),
            FilledButton(
              child: const Text('Clear items'),
              onPressed: () async {
                setState(() {
                  things = null;
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}
Screencast.From.2025-07-20.19-33-24.mp4

After I set the selected and then later set the items, the box is still empty until the listenable is updated again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions