Skip to content

Commit afa3789

Browse files
authored
Users of ChangeNotifier should dispatch event of object creation in constructor. (flutter#133210)
1 parent 8175d69 commit afa3789

File tree

8 files changed

+72
-4
lines changed

8 files changed

+72
-4
lines changed

packages/flutter/lib/src/widgets/navigator.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,6 +3355,13 @@ typedef _IndexWhereCallback = bool Function(_RouteEntry element);
33553355
/// Acts as a ChangeNotifier and notifies after its List of _RouteEntries is
33563356
/// mutated.
33573357
class _History extends Iterable<_RouteEntry> with ChangeNotifier {
3358+
/// Creates an instance of [_History].
3359+
_History() {
3360+
if (kFlutterMemoryAllocationsEnabled) {
3361+
maybeDispatchObjectCreation();
3362+
}
3363+
}
3364+
33583365
final List<_RouteEntry> _value = <_RouteEntry>[];
33593366

33603367
int indexWhere(_IndexWhereCallback test, [int start = 0]) {

packages/flutter/lib/src/widgets/selectable_region.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,13 @@ class _SelectableRegionContainerDelegate extends MultiSelectableSelectionContain
15671567
/// This class optimize the selection update by keeping track of the
15681568
/// [Selectable]s that currently contain the selection edges.
15691569
abstract class MultiSelectableSelectionContainerDelegate extends SelectionContainerDelegate with ChangeNotifier {
1570+
/// Creates an instance of [MultiSelectableSelectionContainerDelegate].
1571+
MultiSelectableSelectionContainerDelegate() {
1572+
if (kFlutterMemoryAllocationsEnabled) {
1573+
maybeDispatchObjectCreation();
1574+
}
1575+
}
1576+
15701577
/// Gets the list of selectables this delegate is managing.
15711578
List<Selectable> selectables = <Selectable>[];
15721579

packages/flutter/lib/src/widgets/widget_inspector.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2895,6 +2895,13 @@ class _WidgetInspectorState extends State<WidgetInspector>
28952895

28962896
/// Mutable selection state of the inspector.
28972897
class InspectorSelection with ChangeNotifier {
2898+
/// Creates an instance of [InspectorSelection].
2899+
InspectorSelection() {
2900+
if (kFlutterMemoryAllocationsEnabled) {
2901+
maybeDispatchObjectCreation();
2902+
}
2903+
}
2904+
28982905
/// Render objects that are candidates to be selected.
28992906
///
29002907
/// Tools may wish to iterate through the list of candidates.

packages/flutter/test/foundation/change_notifier_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class A {
2727
}
2828

2929
class B extends A with ChangeNotifier {
30+
B() {
31+
if (kFlutterMemoryAllocationsEnabled) {
32+
maybeDispatchObjectCreation();
33+
}
34+
}
35+
3036
@override
3137
void test() {
3238
notifyListeners();
@@ -35,6 +41,12 @@ class B extends A with ChangeNotifier {
3541
}
3642

3743
class Counter with ChangeNotifier {
44+
Counter() {
45+
if (kFlutterMemoryAllocationsEnabled) {
46+
maybeDispatchObjectCreation();
47+
}
48+
}
49+
3850
int get value => _value;
3951
int _value = 0;
4052
set value(int value) {

packages/flutter/test/widgets/automatic_keep_alive_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:flutter/foundation.dart';
56
import 'package:flutter/gestures.dart' show DragStartBehavior;
67
import 'package:flutter/rendering.dart';
78
import 'package:flutter/widgets.dart';
@@ -631,6 +632,12 @@ class RenderSliverMultiBoxAdaptorAlt extends RenderSliver with
631632
}
632633

633634
class LeakCheckerHandle with ChangeNotifier {
635+
LeakCheckerHandle() {
636+
if (kFlutterMemoryAllocationsEnabled) {
637+
maybeDispatchObjectCreation();
638+
}
639+
}
640+
634641
@override
635642
bool get hasListeners => super.hasListeners;
636643
}

packages/flutter/test/widgets/route_notification_messages_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,11 @@ class SimpleRouterDelegate extends RouterDelegate<RouteInformation> with ChangeN
338338
required this.builder,
339339
this.onPopRoute,
340340
this.reportConfiguration = false,
341-
});
341+
}) {
342+
if (kFlutterMemoryAllocationsEnabled) {
343+
maybeDispatchObjectCreation();
344+
}
345+
}
342346

343347
RouteInformation get routeInformation => _routeInformation;
344348
late RouteInformation _routeInformation;

packages/flutter/test/widgets/router_restoration_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ class _TestRouteInformationParser extends RouteInformationParser<String> {
9090
}
9191

9292
class _TestRouterDelegate extends RouterDelegate<String> with ChangeNotifier {
93+
_TestRouterDelegate() {
94+
if (kFlutterMemoryAllocationsEnabled) {
95+
maybeDispatchObjectCreation();
96+
}
97+
}
98+
9399
final List<String> newRoutePaths = <String>[];
94100
final List<String> restoredRoutePaths = <String>[];
95101

@@ -128,6 +134,12 @@ class _TestRouterDelegate extends RouterDelegate<String> with ChangeNotifier {
128134
}
129135

130136
class _TestRouteInformationProvider extends RouteInformationProvider with ChangeNotifier {
137+
_TestRouteInformationProvider() {
138+
if (kFlutterMemoryAllocationsEnabled) {
139+
maybeDispatchObjectCreation();
140+
}
141+
}
142+
131143
@override
132144
RouteInformation get value => _value;
133145
RouteInformation _value = RouteInformation(uri: Uri.parse('/home'));

packages/flutter/test/widgets/router_test.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,11 @@ class SimpleRouterDelegate extends RouterDelegate<RouteInformation> with ChangeN
16331633
this.builder,
16341634
this.onPopRoute,
16351635
this.reportConfiguration = false,
1636-
});
1636+
}) {
1637+
if (kFlutterMemoryAllocationsEnabled) {
1638+
maybeDispatchObjectCreation();
1639+
}
1640+
}
16371641

16381642
RouteInformation? get routeInformation => _routeInformation;
16391643
RouteInformation? _routeInformation;
@@ -1717,7 +1721,11 @@ class SimpleNavigatorRouterDelegate extends RouterDelegate<RouteInformation> wit
17171721
class SimpleRouteInformationProvider extends RouteInformationProvider with ChangeNotifier {
17181722
SimpleRouteInformationProvider({
17191723
this.onRouterReport,
1720-
});
1724+
}) {
1725+
if (kFlutterMemoryAllocationsEnabled) {
1726+
maybeDispatchObjectCreation();
1727+
}
1728+
}
17211729

17221730
RouterReportRouterInformation? onRouterReport;
17231731

@@ -1773,7 +1781,11 @@ class CompleterRouteInformationParser extends RouteInformationParser<RouteInform
17731781
class SimpleAsyncRouterDelegate extends RouterDelegate<RouteInformation> with ChangeNotifier {
17741782
SimpleAsyncRouterDelegate({
17751783
required this.builder,
1776-
});
1784+
}) {
1785+
if (kFlutterMemoryAllocationsEnabled) {
1786+
maybeDispatchObjectCreation();
1787+
}
1788+
}
17771789

17781790
RouteInformation? get routeInformation => _routeInformation;
17791791
RouteInformation? _routeInformation;

0 commit comments

Comments
 (0)