Skip to content

DropdownMenu throws exception when it is in any scrollable list view and scrolls quickly #139871

@BppleMan

Description

@BppleMan

Steps to reproduce

  1. Create new flutter project with flutter cli
  2. Edit the body of scaffold with:
ListView.builder(
  itemBuilder: (context, index) {
    return DropdownMenu(dropdownMenuEntries: []);
  },
)
  1. run the project
  2. scroll quickly

Expected results

There shouldn't be any exceptions

Actual results

macos

setState() called after dispose(): _DropdownMenuState<Object?>#bc5c7(lifecycle state: defunct, not mounted)

web (chrome)

setState() called after dispose(): _DropdownMenuState<Object?>#7f4e3(lifecycle state: defunct, not mounted)

ios simulator is correct

Code sample

Code sample
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(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      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> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: ListView.builder(
        itemBuilder: (context, index) {
          return DropdownMenu(dropdownMenuEntries: []);
        },
      ),
    );
  }
}

Logs

Logs

macos

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
setState() called after dispose(): _DropdownMenuState<Object?>#bc5c7(lifecycle state: defunct, not mounted)

This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.

The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

When the exception was thrown, this was the stack: 
#0      State.setState.<anonymous closure> (package:flutter/src/widgets/framework.dart:1167:9)
#1      State.setState (package:flutter/src/widgets/framework.dart:1202:6)
#2      _DropdownMenuState.refreshLeadingPadding.<anonymous closure> (package:flutter/src/material/dropdown_menu.dart:387:7)
#3      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1325:15)
#4      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1264:9)
#5      SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1113:5)
#6      _invoke (dart:ui/hooks.dart:312:13)
#7      PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:383:5)
#8      _drawFrame (dart:ui/hooks.dart:283:31)
====================================================================================================

web (chrome)

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
setState() called after dispose(): _DropdownMenuState<Object?>#7f4e3(lifecycle state: defunct, not mounted)

This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.

The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

When the exception was thrown, this was the stack: 
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3       throw_
packages/flutter/src/widgets/framework.dart 1167:9                                <fn>
packages/flutter/src/widgets/framework.dart 1201:14                               setState
packages/flutter/src/material/dropdown_menu.dart 387:7                            <fn>
packages/flutter/src/scheduler/binding.dart 1325:7                                [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1264:9                                handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1113:5                                [_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1274:5                                invoke
lib/_engine/engine/platform_dispatcher.dart 248:5                                 invokeOnDrawFrame
lib/_engine/engine/initialization.dart 186:36                                     <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 574:37  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 579:39  dcall
====================================================================================================

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.16.3, on macOS 12.6.8 21G725 darwin-x64, locale zh-Hans-CN)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] IntelliJ IDEA Ultimate Edition (version 2023.2.5)
[✓] VS Code (version 1.84.2)
[✓] VS Code (version 1.83.0-insider)
[✓] Connected device (2 available)
[✓] Network resources

! Doctor found issues in 2 categories.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: desktopRunning on desktopf: material designflutter/packages/flutter/material repository.found in release: 3.16Found to occur in 3.16found in release: 3.18Found to occur in 3.18frameworkflutter/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-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions