Skip to content

Commit b8ff1a0

Browse files
matanlureymboetger
authored andcommitted
Replace LinkedHashMap.* with Map.*, which _is_ Linked (flutter#170713)
Pre-work to make it easier to land flutter#170435. The default `Map` constructor is and always has been a `LinkedHashMap`: https://api.dart.dev/dart-core/Map/Map.html This change make its easier to relax the type annotation rules in a future PR. /cc @eernstg
1 parent 66f54db commit b8ff1a0

File tree

7 files changed

+49
-66
lines changed

7 files changed

+49
-66
lines changed

dev/integration_tests/new_gallery/lib/data/demos.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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 'dart:collection';
6-
75
import 'package:flutter/cupertino.dart';
86
import 'package:flutter/material.dart';
97

@@ -110,7 +108,7 @@ Future<void> pumpDeferredLibraries() {
110108
class Demos {
111109
static Map<String?, GalleryDemo> asSlugToDemoMap(BuildContext context) {
112110
final GalleryLocalizations localizations = GalleryLocalizations.of(context)!;
113-
return LinkedHashMap<String?, GalleryDemo>.fromIterable(
111+
return Map<String?, GalleryDemo>.fromIterable(
114112
all(localizations),
115113
// ignore: avoid_dynamic_calls
116114
key: (dynamic demo) => demo.slug as String?,

dev/integration_tests/new_gallery/lib/pages/settings.dart

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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 'dart:collection';
6-
75
import 'package:collection/collection.dart';
86
import 'package:flutter/material.dart';
97
import 'package:flutter_localized_locales/flutter_localized_locales.dart';
@@ -100,9 +98,9 @@ class _SettingsPageState extends State<SettingsPage> {
10098

10199
/// Create a sorted — by native name – map of supported locales to their
102100
/// intended display string, with a system option as the first element.
103-
LinkedHashMap<Locale, DisplayOption> _getLocaleOptions() {
104-
final LinkedHashMap<Locale, DisplayOption> localeOptions =
105-
LinkedHashMap<Locale, DisplayOption>.of(<Locale, DisplayOption>{
101+
Map<Locale, DisplayOption> _getLocaleOptions() {
102+
final Map<Locale, DisplayOption> localeOptions =
103+
Map<Locale, DisplayOption>.of(<Locale, DisplayOption>{
106104
systemLocaleOption: DisplayOption(
107105
GalleryLocalizations.of(context)!.settingsSystemDefault +
108106
(deviceLocale != null
@@ -123,7 +121,7 @@ class _SettingsPageState extends State<SettingsPage> {
123121
compareAsciiUpperCase(l1.value.title, l2.value.title),
124122
);
125123

126-
localeOptions.addAll(LinkedHashMap<Locale, DisplayOption>.fromEntries(displayLocales));
124+
localeOptions.addAll(Map<Locale, DisplayOption>.fromEntries(displayLocales));
127125
return localeOptions;
128126
}
129127

@@ -138,7 +136,7 @@ class _SettingsPageState extends State<SettingsPage> {
138136
SettingsListItem<double?>(
139137
title: localizations.settingsTextScaling,
140138
selectedOption: options.textScaleFactor(context, useSentinel: true),
141-
optionsMap: LinkedHashMap<double?, DisplayOption>.of(<double?, DisplayOption>{
139+
optionsMap: Map<double?, DisplayOption>.of(<double?, DisplayOption>{
142140
systemTextScaleFactorOption: DisplayOption(localizations.settingsSystemDefault),
143141
0.8: DisplayOption(localizations.settingsTextScalingSmall),
144142
1.0: DisplayOption(localizations.settingsTextScalingNormal),
@@ -154,15 +152,14 @@ class _SettingsPageState extends State<SettingsPage> {
154152
SettingsListItem<CustomTextDirection?>(
155153
title: localizations.settingsTextDirection,
156154
selectedOption: options.customTextDirection,
157-
optionsMap: LinkedHashMap<CustomTextDirection?, DisplayOption>.of(
158-
<CustomTextDirection?, DisplayOption>{
159-
CustomTextDirection.localeBased: DisplayOption(
160-
localizations.settingsTextDirectionLocaleBased,
161-
),
162-
CustomTextDirection.ltr: DisplayOption(localizations.settingsTextDirectionLTR),
163-
CustomTextDirection.rtl: DisplayOption(localizations.settingsTextDirectionRTL),
164-
},
165-
),
155+
optionsMap:
156+
Map<CustomTextDirection?, DisplayOption>.of(<CustomTextDirection?, DisplayOption>{
157+
CustomTextDirection.localeBased: DisplayOption(
158+
localizations.settingsTextDirectionLocaleBased,
159+
),
160+
CustomTextDirection.ltr: DisplayOption(localizations.settingsTextDirectionLTR),
161+
CustomTextDirection.rtl: DisplayOption(localizations.settingsTextDirectionRTL),
162+
}),
166163
onOptionChanged:
167164
(CustomTextDirection? newTextDirection) => GalleryOptions.update(
168165
context,
@@ -187,14 +184,13 @@ class _SettingsPageState extends State<SettingsPage> {
187184
SettingsListItem<TargetPlatform?>(
188185
title: localizations.settingsPlatformMechanics,
189186
selectedOption: options.platform,
190-
optionsMap:
191-
LinkedHashMap<TargetPlatform?, DisplayOption>.of(<TargetPlatform?, DisplayOption>{
192-
TargetPlatform.android: DisplayOption('Android'),
193-
TargetPlatform.iOS: DisplayOption('iOS'),
194-
TargetPlatform.macOS: DisplayOption('macOS'),
195-
TargetPlatform.linux: DisplayOption('Linux'),
196-
TargetPlatform.windows: DisplayOption('Windows'),
197-
}),
187+
optionsMap: Map<TargetPlatform?, DisplayOption>.of(<TargetPlatform?, DisplayOption>{
188+
TargetPlatform.android: DisplayOption('Android'),
189+
TargetPlatform.iOS: DisplayOption('iOS'),
190+
TargetPlatform.macOS: DisplayOption('macOS'),
191+
TargetPlatform.linux: DisplayOption('Linux'),
192+
TargetPlatform.windows: DisplayOption('Windows'),
193+
}),
198194
onOptionChanged:
199195
(TargetPlatform? newPlatform) =>
200196
GalleryOptions.update(context, options.copyWith(platform: newPlatform)),
@@ -204,7 +200,7 @@ class _SettingsPageState extends State<SettingsPage> {
204200
SettingsListItem<ThemeMode?>(
205201
title: localizations.settingsTheme,
206202
selectedOption: options.themeMode,
207-
optionsMap: LinkedHashMap<ThemeMode?, DisplayOption>.of(<ThemeMode?, DisplayOption>{
203+
optionsMap: Map<ThemeMode?, DisplayOption>.of(<ThemeMode?, DisplayOption>{
208204
ThemeMode.system: DisplayOption(localizations.settingsSystemDefault),
209205
ThemeMode.dark: DisplayOption(localizations.settingsDarkTheme),
210206
ThemeMode.light: DisplayOption(localizations.settingsLightTheme),

dev/integration_tests/new_gallery/lib/pages/settings_list_item.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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 'dart:collection';
6-
75
import 'package:flutter/material.dart';
86

97
// Common constants between SlowMotionSetting and SettingsListItem.
@@ -81,7 +79,7 @@ class SettingsListItem<T> extends StatefulWidget {
8179
required this.isExpanded,
8280
});
8381

84-
final LinkedHashMap<T, DisplayOption> optionsMap;
82+
final Map<T, DisplayOption> optionsMap;
8583
final String title;
8684
final T selectedOption;
8785
final ValueChanged<T> onOptionChanged;

packages/flutter/lib/src/foundation/observer_list.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class ObserverList<T> extends Iterable<T> {
106106
///
107107
/// * [ObserverList] for a list that is fast for small numbers of observers.
108108
class HashedObserverList<T> extends Iterable<T> {
109-
final LinkedHashMap<T, int> _map = LinkedHashMap<T, int>();
109+
final Map<T, int> _map = <T, int>{};
110110

111111
/// Adds an item to the end of this list.
112112
///

packages/flutter/lib/src/rendering/editable.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/// @docImport 'package:flutter/cupertino.dart';
66
library;
77

8-
import 'dart:collection';
98
import 'dart:math' as math;
109
import 'dart:ui' as ui show BoxHeightStyle, BoxWidthStyle, LineMetrics, SemanticsInputType, TextBox;
1110

@@ -1308,7 +1307,7 @@ class RenderEditable extends RenderBox
13081307
// can be re-used when [assembleSemanticsNode] is called again. This ensures
13091308
// stable ids for the [SemanticsNode]s of [TextSpan]s across
13101309
// [assembleSemanticsNode] invocations.
1311-
LinkedHashMap<Key, SemanticsNode>? _cachedChildNodes;
1310+
Map<Key, SemanticsNode>? _cachedChildNodes;
13121311

13131312
/// Returns a list of rects that bound the given selection, and the text
13141313
/// direction. The text direction is used by the engine to calculate
@@ -1436,7 +1435,7 @@ class RenderEditable extends RenderBox
14361435
int placeholderIndex = 0;
14371436
int childIndex = 0;
14381437
RenderBox? child = firstChild;
1439-
final LinkedHashMap<Key, SemanticsNode> newChildCache = LinkedHashMap<Key, SemanticsNode>();
1438+
final Map<Key, SemanticsNode> newChildCache = <Key, SemanticsNode>{};
14401439
_cachedCombinedSemanticsInfos ??= combineSemanticsInfo(_semanticsInfo!);
14411440
for (final InlineSpanSemanticsInformation info in _cachedCombinedSemanticsInfos!) {
14421441
final TextSelection selection = TextSelection(

packages/flutter/lib/src/rendering/mouse_tracker.dart

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/// @docImport 'binding.dart';
66
library;
77

8-
import 'dart:collection' show LinkedHashMap;
98
import 'dart:ui';
109

1110
import 'package:flutter/foundation.dart';
@@ -27,16 +26,13 @@ class _MouseState {
2726
_MouseState({required PointerEvent initialEvent}) : _latestEvent = initialEvent;
2827

2928
// The list of annotations that contains this device.
30-
//
31-
// It uses [LinkedHashMap] to keep the insertion order.
32-
LinkedHashMap<MouseTrackerAnnotation, Matrix4> get annotations => _annotations;
33-
LinkedHashMap<MouseTrackerAnnotation, Matrix4> _annotations =
34-
LinkedHashMap<MouseTrackerAnnotation, Matrix4>();
29+
Map<MouseTrackerAnnotation, Matrix4> get annotations => _annotations;
30+
Map<MouseTrackerAnnotation, Matrix4> _annotations = <MouseTrackerAnnotation, Matrix4>{};
3531

36-
LinkedHashMap<MouseTrackerAnnotation, Matrix4> replaceAnnotations(
37-
LinkedHashMap<MouseTrackerAnnotation, Matrix4> value,
32+
Map<MouseTrackerAnnotation, Matrix4> replaceAnnotations(
33+
Map<MouseTrackerAnnotation, Matrix4> value,
3834
) {
39-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> previous = _annotations;
35+
final Map<MouseTrackerAnnotation, Matrix4> previous = _annotations;
4036
_annotations = value;
4137
return previous;
4238
}
@@ -93,12 +89,12 @@ class _MouseTrackerUpdateDetails with Diagnosticable {
9389
/// The annotations that the device is hovering before the update.
9490
///
9591
/// It is never null.
96-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations;
92+
final Map<MouseTrackerAnnotation, Matrix4> lastAnnotations;
9793

9894
/// The annotations that the device is hovering after the update.
9995
///
10096
/// It is never null.
101-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations;
97+
final Map<MouseTrackerAnnotation, Matrix4> nextAnnotations;
10298

10399
/// The last event that the device observed before the update.
104100
///
@@ -232,11 +228,8 @@ class MouseTracker extends ChangeNotifier {
232228
lastEvent.position != event.position;
233229
}
234230

235-
LinkedHashMap<MouseTrackerAnnotation, Matrix4> _hitTestInViewResultToAnnotations(
236-
HitTestResult result,
237-
) {
238-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> annotations =
239-
LinkedHashMap<MouseTrackerAnnotation, Matrix4>();
231+
Map<MouseTrackerAnnotation, Matrix4> _hitTestInViewResultToAnnotations(HitTestResult result) {
232+
final Map<MouseTrackerAnnotation, Matrix4> annotations = <MouseTrackerAnnotation, Matrix4>{};
240233
for (final HitTestEntry entry in result.path) {
241234
final Object target = entry.target;
242235
if (target is MouseTrackerAnnotation) {
@@ -251,12 +244,12 @@ class MouseTracker extends ChangeNotifier {
251244
//
252245
// If the device is not connected or not a mouse, an empty map is returned
253246
// without calling `hitTest`.
254-
LinkedHashMap<MouseTrackerAnnotation, Matrix4> _findAnnotations(_MouseState state) {
247+
Map<MouseTrackerAnnotation, Matrix4> _findAnnotations(_MouseState state) {
255248
final Offset globalPosition = state.latestEvent.position;
256249
final int device = state.device;
257250
final int viewId = state.latestEvent.viewId;
258251
if (!_mouseStates.containsKey(device)) {
259-
return LinkedHashMap<MouseTrackerAnnotation, Matrix4>();
252+
return <MouseTrackerAnnotation, Matrix4>{};
260253
}
261254

262255
return _hitTestInViewResultToAnnotations(_hitTestInView(globalPosition, viewId));
@@ -341,12 +334,13 @@ class MouseTracker extends ChangeNotifier {
341334
final _MouseState targetState = _mouseStates[device] ?? existingState!;
342335

343336
final PointerEvent lastEvent = targetState.replaceLatestEvent(event);
344-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations =
337+
final Map<MouseTrackerAnnotation, Matrix4> nextAnnotations =
345338
event is PointerRemovedEvent
346-
? LinkedHashMap<MouseTrackerAnnotation, Matrix4>()
339+
? <MouseTrackerAnnotation, Matrix4>{}
347340
: _hitTestInViewResultToAnnotations(result);
348-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = targetState
349-
.replaceAnnotations(nextAnnotations);
341+
final Map<MouseTrackerAnnotation, Matrix4> lastAnnotations = targetState.replaceAnnotations(
342+
nextAnnotations,
343+
);
350344

351345
_handleDeviceUpdate(
352346
_MouseTrackerUpdateDetails.byPointerEvent(
@@ -374,11 +368,10 @@ class MouseTracker extends ChangeNotifier {
374368
_deviceUpdatePhase(() {
375369
for (final _MouseState dirtyState in _mouseStates.values) {
376370
final PointerEvent lastEvent = dirtyState.latestEvent;
377-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations = _findAnnotations(
378-
dirtyState,
371+
final Map<MouseTrackerAnnotation, Matrix4> nextAnnotations = _findAnnotations(dirtyState);
372+
final Map<MouseTrackerAnnotation, Matrix4> lastAnnotations = dirtyState.replaceAnnotations(
373+
nextAnnotations,
379374
);
380-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = dirtyState
381-
.replaceAnnotations(nextAnnotations);
382375

383376
_handleDeviceUpdate(
384377
_MouseTrackerUpdateDetails.byNewFrame(
@@ -407,8 +400,8 @@ class MouseTracker extends ChangeNotifier {
407400
static void _handleDeviceUpdateMouseEvents(_MouseTrackerUpdateDetails details) {
408401
final PointerEvent latestEvent = details.latestEvent;
409402

410-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> lastAnnotations = details.lastAnnotations;
411-
final LinkedHashMap<MouseTrackerAnnotation, Matrix4> nextAnnotations = details.nextAnnotations;
403+
final Map<MouseTrackerAnnotation, Matrix4> lastAnnotations = details.lastAnnotations;
404+
final Map<MouseTrackerAnnotation, Matrix4> nextAnnotations = details.nextAnnotations;
412405

413406
// Order is important for mouse event callbacks. The
414407
// `_hitTestInViewResultToAnnotations` returns annotations in the visual order

packages/flutter/lib/src/rendering/paragraph.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
/// @docImport 'editable.dart';
88
library;
99

10-
import 'dart:collection';
1110
import 'dart:math' as math;
1211
import 'dart:ui'
1312
as ui
@@ -1268,7 +1267,7 @@ class RenderParagraph extends RenderBox
12681267
// can be re-used when [assembleSemanticsNode] is called again. This ensures
12691268
// stable ids for the [SemanticsNode]s of [TextSpan]s across
12701269
// [assembleSemanticsNode] invocations.
1271-
LinkedHashMap<Key, SemanticsNode>? _cachedChildNodes;
1270+
Map<Key, SemanticsNode>? _cachedChildNodes;
12721271

12731272
@override
12741273
void assembleSemanticsNode(
@@ -1285,7 +1284,7 @@ class RenderParagraph extends RenderBox
12851284
int placeholderIndex = 0;
12861285
int childIndex = 0;
12871286
RenderBox? child = firstChild;
1288-
final LinkedHashMap<Key, SemanticsNode> newChildCache = LinkedHashMap<Key, SemanticsNode>();
1287+
final Map<Key, SemanticsNode> newChildCache = <Key, SemanticsNode>{};
12891288
_cachedCombinedSemanticsInfos ??= combineSemanticsInfo(_semanticsInfo!);
12901289
for (final InlineSpanSemanticsInformation info in _cachedCombinedSemanticsInfos!) {
12911290
final TextSelection selection = TextSelection(

0 commit comments

Comments
 (0)