Skip to content

Commit

Permalink
Handle deprecation in Flutter. (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c authored Jan 3, 2024
1 parent 3d4c0d6 commit 6591934
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
3 changes: 2 additions & 1 deletion examples/leak_tracking/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:leak_tracker/leak_tracker.dart';

void main() {
LeakTracking.start();
MemoryAllocations.instance.addListener(
// Dispatch memory events from the Flutter engine to LeakTracking.
FlutterMemoryAllocations.instance.addListener(
(ObjectEvent event) => LeakTracking.dispatchObjectEvent(event.toMap()),
);

Expand Down
4 changes: 4 additions & 0 deletions pkgs/leak_tracker_flutter_testing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.2

* Replaced depracated `MemoryAllocations` with `FlutterMemoryAllocations`.

## 2.0.1

* Upgrade to leak_tracker 10.0.0 and leak_tracker_testing 2.0.1.
Expand Down
6 changes: 3 additions & 3 deletions pkgs/leak_tracker_flutter_testing/lib/src/matchers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:flutter/foundation.dart';
import 'package:matcher/matcher.dart';

/// Invokes [callback] and collects
/// events dispatched to [MemoryAllocations.instance] for [type].
/// events dispatched to [FlutterMemoryAllocations.instance] for [type].
Future<List<ObjectEvent>> memoryEvents(
FutureOr<void> Function() callback,
Type type,
Expand All @@ -21,9 +21,9 @@ Future<List<ObjectEvent>> memoryEvents(
}
}

MemoryAllocations.instance.addListener(listener);
FlutterMemoryAllocations.instance.addListener(listener);
await callback();
MemoryAllocations.instance.removeListener(listener);
FlutterMemoryAllocations.instance.removeListener(listener);

return events;
}
Expand Down
6 changes: 4 additions & 2 deletions pkgs/leak_tracker_flutter_testing/lib/src/testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Future<void> maybeTearDownLeakTrackingForAll() async {

// The listener is not added/removed for each test,
// because GC may happen after test is complete.
MemoryAllocations.instance.removeListener(_dispatchFlutterEventToLeakTracker);
FlutterMemoryAllocations.instance
.removeListener(_dispatchFlutterEventToLeakTracker);
await forceGC(fullGcCycles: defaultNumberOfGcCycles);
LeakTracking.declareNotDisposedObjectsAsLeaks();
final leaks = await LeakTracking.collectLeaks();
Expand Down Expand Up @@ -107,5 +108,6 @@ void _maybeStartLeakTracking() {

LeakTracking.phase = const PhaseSettings.ignored();
LeakTracking.start(config: LeakTrackingConfig.passive());
MemoryAllocations.instance.addListener(_dispatchFlutterEventToLeakTracker);
FlutterMemoryAllocations.instance
.addListener(_dispatchFlutterEventToLeakTracker);
}
5 changes: 3 additions & 2 deletions pkgs/leak_tracker_flutter_testing/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: leak_tracker_flutter_testing
version: 2.0.1
version: 2.0.2
description: An internal package to test leak tracking with Flutter.
repository: https://github.com/dart-lang/leak_tracker/tree/main/pkgs/leak_tracker_flutter_testing

environment:
sdk: '>=3.1.2 <4.0.0'
sdk: '>=3.2.0 <4.0.0'
flutter: '>=3.18.0-18.0.pre.54'

dependencies:
flutter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {
test('dispatchObjectEvent dispatches Flutter SDK instrumentation.', () {
final tracker = EventTracker();

MemoryAllocations.instance.addListener(
FlutterMemoryAllocations.instance.addListener(
(event) => dispatchObjectEvent(
event.toMap(),
onStartTracking: tracker.dispatchObjectCreated,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import 'package:test/test.dart';

class _TrackedClass {
_TrackedClass() {
MemoryAllocations.instance.dispatchObjectCreated(
FlutterMemoryAllocations.instance.dispatchObjectCreated(
library: 'library',
className: '_TrackedClass',
object: this,
);
}

void dispose() {
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
}
}

Expand Down

0 comments on commit 6591934

Please sign in to comment.