Skip to content

RangeError when both removing listener and disposing ValueNotifier from listener callback #114224

@spkersten

Description

@spkersten

This code

void main() {
  final vn = ValueNotifier<int>(0);

  void foo() {
    vn.removeListener(foo); // A
    vn.dispose(); // B
  }

  vn.addListener(foo);

  vn.value = 3;
}

results in:

flutter: RangeError (length): Invalid value: Not in inclusive range 0..576460752303423487: -1, #0      new _List (dart:core-patch/array.dart)
#1      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:384:50)
#2      ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:463:5)

while I would expect it to work. There is no exception when either line A or line B is removed.

Issue can be fixed by setting also _reentrantlyRemovedListeners to zero in dispose here:

[!] Flutter (Channel unknown, 3.3.2, on macOS 12.5.1 21G83 darwin-arm, locale en-NL)
    ! Flutter version 3.3.2 on channel unknown at /.../flutter
    ! Upstream repository unknown
    • Framework revision e3c29ec00c (6 weeks ago), 2022-09-14 08:46:55 -0500
    • Engine revision a4ff2c53d8
    • Dart version 2.18.1
    • DevTools version 2.15.0

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listc: crashStack traces logged to the consolefound in release: 3.3Found to occur in 3.3found in release: 3.5Found to occur in 3.5frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions