[flutter_tools] migrate context to null safety (#78359)
This commit is contained in:
@@ -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 <Type, Generator>{},
|
||||
]);
|
||||
|
||||
final String name;
|
||||
final AppContext _parent;
|
||||
final String? name;
|
||||
final AppContext? _parent;
|
||||
final Map<Type, Generator> _overrides;
|
||||
final Map<Type, Generator> _fallbacks;
|
||||
final Map<Type, dynamic> _values = <Type, dynamic>{};
|
||||
|
||||
List<Type> _reentrantChecks;
|
||||
List<Type>? _reentrantChecks;
|
||||
|
||||
/// Bootstrap context.
|
||||
static final AppContext _root = AppContext._(null, 'ROOT');
|
||||
@@ -94,19 +92,19 @@ class AppContext {
|
||||
return _values.putIfAbsent(type, () {
|
||||
_reentrantChecks ??= <Type>[];
|
||||
|
||||
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<Type>(_reentrantChecks.sublist(index)));
|
||||
UnmodifiableListView<Type>(_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<T>() {
|
||||
dynamic value = _generateIfNecessary(T, _overrides);
|
||||
if (value == null && _parent != null) {
|
||||
value = _parent.get<T>();
|
||||
value = _parent!.get<T>();
|
||||
}
|
||||
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<V> run<V>({
|
||||
@required FutureOr<V> Function() body,
|
||||
String name,
|
||||
Map<Type, Generator> overrides,
|
||||
Map<Type, Generator> fallbacks,
|
||||
ZoneSpecification zoneSpecification,
|
||||
required FutureOr<V> Function() body,
|
||||
String? name,
|
||||
Map<Type, Generator>? overrides,
|
||||
Map<Type, Generator>? 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) {
|
||||
|
||||
Reference in New Issue
Block a user