1
- import 'package:flutter/cupertino .dart' ;
1
+ import 'package:flutter/foundation .dart' ;
2
2
import 'package:wiredash/src/utils/object_util.dart' ;
3
3
4
4
/// A [ChangeNotifier] but better
@@ -7,16 +7,34 @@ import 'package:wiredash/src/utils/object_util.dart';
7
7
/// - Doesn't crash when [dispose] is called multiple times
8
8
/// - Knows when disposed [isDisposed]
9
9
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
+
10
17
bool _isDisposed = false ;
18
+
11
19
bool get isDisposed => _isDisposed;
12
20
13
21
final List <void Function ()> listeners = [];
14
22
23
+ ChangeNotifier2 () {
24
+ if (kFlutterMemoryAllocationsEnabled) {
25
+ maybeDispatchObjectCreation ();
26
+ }
27
+ }
28
+
15
29
@override
16
30
void addListener (void Function () listener) {
17
31
if (isDisposed) {
18
32
throw "$instanceName is already disposed." ;
19
33
}
34
+
35
+ if (kFlutterMemoryAllocationsEnabled) {
36
+ maybeDispatchObjectCreation ();
37
+ }
20
38
listeners.add (listener);
21
39
}
22
40
@@ -36,6 +54,9 @@ class ChangeNotifier2 implements ChangeNotifier {
36
54
}
37
55
_isDisposed = true ;
38
56
listeners.clear ();
57
+ if (kFlutterMemoryAllocationsEnabled && _creationDispatched) {
58
+ MemoryAllocations .instance.dispatchObjectDisposed (object: this );
59
+ }
39
60
}
40
61
41
62
@override
@@ -56,4 +77,18 @@ class ChangeNotifier2 implements ChangeNotifier {
56
77
}
57
78
notifyListeners ();
58
79
}
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
+ }
59
94
}
0 commit comments