Skip to content

CupertinoContextMenu is using setState after it was unmounted #124597

@yevyevyev

Description

@yevyevyev

In order to reproduce, delete a few items from the context menu.

Minimal reproducible code
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(
    home: Scaffold(
      body: SomeStateful(),
    ),
  ));
}

class SomeStateful extends StatefulWidget {
  const SomeStateful({super.key});

  @override
  State<SomeStateful> createState() => _SomeStatefulState();
}

class _SomeStatefulState extends State<SomeStateful> {
  final items = List.generate(2, (index) => index).toList();

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: items
          .map((e) => CupertinoContextMenu(
                  actions: [
                    CupertinoContextMenuAction(
                      child: const Text('DELETE'),
                      onPressed: () {
                        setState(() {
                          items.remove(e);
                        });
                        Navigator.of(context).pop();
                      },
                    )
                  ],
                  child: SizedBox(
                    width: MediaQuery.of(context).size.width,
                    child: Material(
                      child: ListTile(
                        title: Text('Item $e'),
                      ),
                    ),
                  )))
          .toList(),
    );
  }
}
Code block where the exception happens context_menu.dart
void _routeAnimationStatusListener(AnimationStatus status) {
    if (status != AnimationStatus.dismissed) {
      return;
    }
   // here the CupertinoContextMenu state is unmounted but we call setState
    setState(() {
      _childHidden = false;
    });
    _route!.animation!.removeStatusListener(_routeAnimationStatusListener);
    _route = null;
  }

Metadata

Metadata

Assignees

Labels

a: error messageError messages from the Flutter frameworkf: cupertinoflutter/packages/flutter/cupertino repositoryfound in release: 3.10Found to occur in 3.10frameworkflutter/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 version

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions