Unverified Commit d27666e0 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] migrate context to null safety (#78359)

parent cdfe3955
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// @dart = 2.8
import 'dart:async'; import 'dart:async';
import 'dart:collection'; import 'dart:collection';
...@@ -39,7 +37,7 @@ const Object contextKey = _Key.key; ...@@ -39,7 +37,7 @@ const Object contextKey = _Key.key;
/// context will not have any values associated with it. /// context will not have any values associated with it.
/// ///
/// This is guaranteed to never return `null`. /// 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 /// A lookup table (mapping types to values) and an implied scope, in which
/// code is run. /// code is run.
...@@ -58,13 +56,13 @@ class AppContext { ...@@ -58,13 +56,13 @@ class AppContext {
this._fallbacks = const <Type, Generator>{}, this._fallbacks = const <Type, Generator>{},
]); ]);
final String name; final String? name;
final AppContext _parent; final AppContext? _parent;
final Map<Type, Generator> _overrides; final Map<Type, Generator> _overrides;
final Map<Type, Generator> _fallbacks; final Map<Type, Generator> _fallbacks;
final Map<Type, dynamic> _values = <Type, dynamic>{}; final Map<Type, dynamic> _values = <Type, dynamic>{};
List<Type> _reentrantChecks; List<Type>? _reentrantChecks;
/// Bootstrap context. /// Bootstrap context.
static final AppContext _root = AppContext._(null, 'ROOT'); static final AppContext _root = AppContext._(null, 'ROOT');
...@@ -94,19 +92,19 @@ class AppContext { ...@@ -94,19 +92,19 @@ class AppContext {
return _values.putIfAbsent(type, () { return _values.putIfAbsent(type, () {
_reentrantChecks ??= <Type>[]; _reentrantChecks ??= <Type>[];
final int index = _reentrantChecks.indexOf(type); final int index = _reentrantChecks!.indexOf(type);
if (index >= 0) { if (index >= 0) {
// We're already in the process of trying to generate this type. // We're already in the process of trying to generate this type.
throw ContextDependencyCycleException._( throw ContextDependencyCycleException._(
UnmodifiableListView<Type>(_reentrantChecks.sublist(index))); UnmodifiableListView<Type>(_reentrantChecks!.sublist(index)));
} }
_reentrantChecks.add(type); _reentrantChecks!.add(type);
try { try {
return _boxNull(generators[type]()); return _boxNull(generators[type]!());
} finally { } finally {
_reentrantChecks.removeLast(); _reentrantChecks!.removeLast();
if (_reentrantChecks.isEmpty) { if (_reentrantChecks!.isEmpty) {
_reentrantChecks = null; _reentrantChecks = null;
} }
} }
...@@ -118,7 +116,7 @@ class AppContext { ...@@ -118,7 +116,7 @@ class AppContext {
T get<T>() { T get<T>() {
dynamic value = _generateIfNecessary(T, _overrides); dynamic value = _generateIfNecessary(T, _overrides);
if (value == null && _parent != null) { if (value == null && _parent != null) {
value = _parent.get<T>(); value = _parent!.get<T>();
} }
return _unboxNull(value ?? _generateIfNecessary(T, _fallbacks)) as T; return _unboxNull(value ?? _generateIfNecessary(T, _fallbacks)) as T;
} }
...@@ -136,11 +134,11 @@ class AppContext { ...@@ -136,11 +134,11 @@ class AppContext {
/// name. This is useful for debugging purposes and is analogous to naming a /// name. This is useful for debugging purposes and is analogous to naming a
/// thread in Java. /// thread in Java.
Future<V> run<V>({ Future<V> run<V>({
@required FutureOr<V> Function() body, required FutureOr<V> Function() body,
String name, String? name,
Map<Type, Generator> overrides, Map<Type, Generator>? overrides,
Map<Type, Generator> fallbacks, Map<Type, Generator>? fallbacks,
ZoneSpecification zoneSpecification, ZoneSpecification? zoneSpecification,
}) async { }) async {
final AppContext child = AppContext._( final AppContext child = AppContext._(
this, this,
...@@ -159,7 +157,7 @@ class AppContext { ...@@ -159,7 +157,7 @@ class AppContext {
String toString() { String toString() {
final StringBuffer buf = StringBuffer(); final StringBuffer buf = StringBuffer();
String indent = ''; String indent = '';
AppContext ctx = this; AppContext? ctx = this;
while (ctx != null) { while (ctx != null) {
buf.write('AppContext'); buf.write('AppContext');
if (ctx.name != null) { if (ctx.name != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment