Skip to content

Commit

Permalink
Users of ChangeNotifier should dispatch event of object creation in c…
Browse files Browse the repository at this point in the history
…onstructor. (#133210)
  • Loading branch information
polina-c authored Aug 24, 2023
1 parent 8175d69 commit afa3789
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/flutter/lib/src/widgets/navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3355,6 +3355,13 @@ typedef _IndexWhereCallback = bool Function(_RouteEntry element);
/// Acts as a ChangeNotifier and notifies after its List of _RouteEntries is
/// mutated.
class _History extends Iterable<_RouteEntry> with ChangeNotifier {
/// Creates an instance of [_History].
_History() {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

final List<_RouteEntry> _value = <_RouteEntry>[];

int indexWhere(_IndexWhereCallback test, [int start = 0]) {
Expand Down
7 changes: 7 additions & 0 deletions packages/flutter/lib/src/widgets/selectable_region.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,13 @@ class _SelectableRegionContainerDelegate extends MultiSelectableSelectionContain
/// This class optimize the selection update by keeping track of the
/// [Selectable]s that currently contain the selection edges.
abstract class MultiSelectableSelectionContainerDelegate extends SelectionContainerDelegate with ChangeNotifier {
/// Creates an instance of [MultiSelectableSelectionContainerDelegate].
MultiSelectableSelectionContainerDelegate() {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

/// Gets the list of selectables this delegate is managing.
List<Selectable> selectables = <Selectable>[];

Expand Down
7 changes: 7 additions & 0 deletions packages/flutter/lib/src/widgets/widget_inspector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2895,6 +2895,13 @@ class _WidgetInspectorState extends State<WidgetInspector>

/// Mutable selection state of the inspector.
class InspectorSelection with ChangeNotifier {
/// Creates an instance of [InspectorSelection].
InspectorSelection() {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

/// Render objects that are candidates to be selected.
///
/// Tools may wish to iterate through the list of candidates.
Expand Down
12 changes: 12 additions & 0 deletions packages/flutter/test/foundation/change_notifier_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class A {
}

class B extends A with ChangeNotifier {
B() {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

@override
void test() {
notifyListeners();
Expand All @@ -35,6 +41,12 @@ class B extends A with ChangeNotifier {
}

class Counter with ChangeNotifier {
Counter() {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

int get value => _value;
int _value = 0;
set value(int value) {
Expand Down
7 changes: 7 additions & 0 deletions packages/flutter/test/widgets/automatic_keep_alive_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart' show DragStartBehavior;
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
Expand Down Expand Up @@ -631,6 +632,12 @@ class RenderSliverMultiBoxAdaptorAlt extends RenderSliver with
}

class LeakCheckerHandle with ChangeNotifier {
LeakCheckerHandle() {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

@override
bool get hasListeners => super.hasListeners;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,11 @@ class SimpleRouterDelegate extends RouterDelegate<RouteInformation> with ChangeN
required this.builder,
this.onPopRoute,
this.reportConfiguration = false,
});
}) {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

RouteInformation get routeInformation => _routeInformation;
late RouteInformation _routeInformation;
Expand Down
12 changes: 12 additions & 0 deletions packages/flutter/test/widgets/router_restoration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class _TestRouteInformationParser extends RouteInformationParser<String> {
}

class _TestRouterDelegate extends RouterDelegate<String> with ChangeNotifier {
_TestRouterDelegate() {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

final List<String> newRoutePaths = <String>[];
final List<String> restoredRoutePaths = <String>[];

Expand Down Expand Up @@ -128,6 +134,12 @@ class _TestRouterDelegate extends RouterDelegate<String> with ChangeNotifier {
}

class _TestRouteInformationProvider extends RouteInformationProvider with ChangeNotifier {
_TestRouteInformationProvider() {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

@override
RouteInformation get value => _value;
RouteInformation _value = RouteInformation(uri: Uri.parse('/home'));
Expand Down
18 changes: 15 additions & 3 deletions packages/flutter/test/widgets/router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1633,7 +1633,11 @@ class SimpleRouterDelegate extends RouterDelegate<RouteInformation> with ChangeN
this.builder,
this.onPopRoute,
this.reportConfiguration = false,
});
}) {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

RouteInformation? get routeInformation => _routeInformation;
RouteInformation? _routeInformation;
Expand Down Expand Up @@ -1717,7 +1721,11 @@ class SimpleNavigatorRouterDelegate extends RouterDelegate<RouteInformation> wit
class SimpleRouteInformationProvider extends RouteInformationProvider with ChangeNotifier {
SimpleRouteInformationProvider({
this.onRouterReport,
});
}) {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

RouterReportRouterInformation? onRouterReport;

Expand Down Expand Up @@ -1773,7 +1781,11 @@ class CompleterRouteInformationParser extends RouteInformationParser<RouteInform
class SimpleAsyncRouterDelegate extends RouterDelegate<RouteInformation> with ChangeNotifier {
SimpleAsyncRouterDelegate({
required this.builder,
});
}) {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}

RouteInformation? get routeInformation => _routeInformation;
RouteInformation? _routeInformation;
Expand Down

0 comments on commit afa3789

Please sign in to comment.