Skip to content

Commit b950862

Browse files
committed
Fix ChangeNotifier compilation error
Caused by: flutter/flutter#133060
1 parent 12973a8 commit b950862

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

lib/src/utils/changenotifier2.dart

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:flutter/cupertino.dart';
1+
import 'package:flutter/foundation.dart';
22
import 'package:wiredash/src/utils/object_util.dart';
33

44
/// A [ChangeNotifier] but better
@@ -7,16 +7,34 @@ import 'package:wiredash/src/utils/object_util.dart';
77
/// - Doesn't crash when [dispose] is called multiple times
88
/// - Knows when disposed [isDisposed]
99
class ChangeNotifier2 implements ChangeNotifier {
10+
/// If true, the event [ObjectCreated] for this instance was dispatched to
11+
/// [MemoryAllocations].
12+
///
13+
/// As [ChangedNotifier] is used as mixin, it does not have constructor,
14+
/// so we use [addListener] to dispatch the event.
15+
bool _creationDispatched = false;
16+
1017
bool _isDisposed = false;
18+
1119
bool get isDisposed => _isDisposed;
1220

1321
final List<void Function()> listeners = [];
1422

23+
ChangeNotifier2() {
24+
if (kFlutterMemoryAllocationsEnabled) {
25+
maybeDispatchObjectCreation();
26+
}
27+
}
28+
1529
@override
1630
void addListener(void Function() listener) {
1731
if (isDisposed) {
1832
throw "$instanceName is already disposed.";
1933
}
34+
35+
if (kFlutterMemoryAllocationsEnabled) {
36+
maybeDispatchObjectCreation();
37+
}
2038
listeners.add(listener);
2139
}
2240

@@ -36,6 +54,9 @@ class ChangeNotifier2 implements ChangeNotifier {
3654
}
3755
_isDisposed = true;
3856
listeners.clear();
57+
if (kFlutterMemoryAllocationsEnabled && _creationDispatched) {
58+
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
59+
}
3960
}
4061

4162
@override
@@ -56,4 +77,18 @@ class ChangeNotifier2 implements ChangeNotifier {
5677
}
5778
notifyListeners();
5879
}
80+
81+
@override
82+
void maybeDispatchObjectCreation() {
83+
// Tree shaker does not include this method and the class MemoryAllocations
84+
// if kFlutterMemoryAllocationsEnabled is false.
85+
if (kFlutterMemoryAllocationsEnabled && !_creationDispatched) {
86+
MemoryAllocations.instance.dispatchObjectCreated(
87+
library: 'package:wiredash/src/utils/changenotifier2.dart',
88+
className: '$ChangeNotifier2',
89+
object: this,
90+
);
91+
_creationDispatched = true;
92+
}
93+
}
5994
}

0 commit comments

Comments
 (0)