From d27666e0581c887550461fc945282a437339dfb9 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Thu, 18 Mar 2021 10:18:20 -0700 Subject: [PATCH] [flutter_tools] migrate context to null safety (#78359) --- .../flutter_tools/lib/src/base/context.dart | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/context.dart b/packages/flutter_tools/lib/src/base/context.dart index 7917e870da..2b63d28088 100644 --- a/packages/flutter_tools/lib/src/base/context.dart +++ b/packages/flutter_tools/lib/src/base/context.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:async'; import 'dart:collection'; @@ -39,7 +37,7 @@ const Object contextKey = _Key.key; /// context will not have any values associated with it. /// /// This is guaranteed to never return `null`. -AppContext get context => Zone.current[contextKey] as AppContext ?? AppContext._root; +AppContext get context => Zone.current[contextKey] as AppContext? ?? AppContext._root; /// A lookup table (mapping types to values) and an implied scope, in which /// code is run. @@ -58,13 +56,13 @@ class AppContext { this._fallbacks = const {}, ]); - final String name; - final AppContext _parent; + final String? name; + final AppContext? _parent; final Map _overrides; final Map _fallbacks; final Map _values = {}; - List _reentrantChecks; + List? _reentrantChecks; /// Bootstrap context. static final AppContext _root = AppContext._(null, 'ROOT'); @@ -94,19 +92,19 @@ class AppContext { return _values.putIfAbsent(type, () { _reentrantChecks ??= []; - final int index = _reentrantChecks.indexOf(type); + final int index = _reentrantChecks!.indexOf(type); if (index >= 0) { // We're already in the process of trying to generate this type. throw ContextDependencyCycleException._( - UnmodifiableListView(_reentrantChecks.sublist(index))); + UnmodifiableListView(_reentrantChecks!.sublist(index))); } - _reentrantChecks.add(type); + _reentrantChecks!.add(type); try { - return _boxNull(generators[type]()); + return _boxNull(generators[type]!()); } finally { - _reentrantChecks.removeLast(); - if (_reentrantChecks.isEmpty) { + _reentrantChecks!.removeLast(); + if (_reentrantChecks!.isEmpty) { _reentrantChecks = null; } } @@ -118,7 +116,7 @@ class AppContext { T get() { dynamic value = _generateIfNecessary(T, _overrides); if (value == null && _parent != null) { - value = _parent.get(); + value = _parent!.get(); } return _unboxNull(value ?? _generateIfNecessary(T, _fallbacks)) as T; } @@ -136,11 +134,11 @@ class AppContext { /// name. This is useful for debugging purposes and is analogous to naming a /// thread in Java. Future run({ - @required FutureOr Function() body, - String name, - Map overrides, - Map fallbacks, - ZoneSpecification zoneSpecification, + required FutureOr Function() body, + String? name, + Map? overrides, + Map? fallbacks, + ZoneSpecification? zoneSpecification, }) async { final AppContext child = AppContext._( this, @@ -159,7 +157,7 @@ class AppContext { String toString() { final StringBuffer buf = StringBuffer(); String indent = ''; - AppContext ctx = this; + AppContext? ctx = this; while (ctx != null) { buf.write('AppContext'); if (ctx.name != null) {