今天使用Future.delayed
这个方法的时候发现第二个参数是FutureOr
,然后吧,觉得没什么大不了的只是一个普通类而已,但是用的时候发现了区别,测试代码如下:
import 'dart:async';
main() {
test(FutureOr<String> computation()) {}
test(() async {
return "aa";
});
test(() {
return "abc";
});
}
看到没,test
方法的参数是FutureOr<String>
类型,但是我可以返回Future<String>
也可以返回String
,是不是对于平时使用java或者kotlin的人来说比较奇怪?然后我们可以跳转到FutureOr
类里看一下这个类上面的文档描述,会发现这么一段话,我选重要的贴一下
///A type representing values that are either `Future<T>` or `T`.
翻译:这个类型相当于`Future<T>` 或者 `T`
算了我比较懒。。。我全贴出来自己看吧
/// A type representing values that are either `Future<T>` or `T`.
///
/// This class declaration is a public stand-in for an internal
/// future-or-value generic type. References to this class are resolved to the
/// internal type.
///
/// It is a compile-time error for any class to extend, mix in or implement
/// `FutureOr`.
///
/// Note: the `FutureOr<T>` type is interpreted as `dynamic` in non strong-mode.
///
/// # Examples
/// ``` dart
/// // The `Future<T>.then` function takes a callback [f] that returns either
/// // an `S` or a `Future<S>`.
/// Future<S> then<S>(FutureOr<S> f(T x), ...);
///
/// // `Completer<T>.complete` takes either a `T` or `Future<T>`.
/// void complete(FutureOr<T> value);
/// ```
///
/// # Advanced
/// The `FutureOr<int>` type is actually the "type union" of the types `int` and
/// `Future<int>`. This type union is defined in such a way that
/// `FutureOr<Object>` is both a super- and sub-type of `Object` (sub-type
/// because `Object` is one of the types of the union, super-type because
/// `Object` is a super-type of both of the types of the union). Together it
/// means that `FutureOr<Object>` is equivalent to `Object`.
///
/// As a corollary, `FutureOr<Object>` is equivalent to
/// `FutureOr<FutureOr<Object>>`, `FutureOr<Future<Object>>` is equivalent to
/// `Future<Object>`.
简而言之,这个类型可以当做Future<T>
或者T
类型,所以在有些时候要注意,在使用FutureOr<T>
执行任何有用的操作之前,通常需要检查是否有Future<T>
或明确的T
。如果type
参数是某个特定类型,如FutureOr<int>
,使用哪个测试无关紧要,是int
还是Future<int>
。 两者都有效,因为这两种类型是不相交的。
但是,如果值类型是Object
或可能使用Object
实例化的类型参数,则两个分支重叠。 Future<Object>
本身实现了Object
,因此是Object
或者是T
,其中T
是一些可以用Object
实例化的类型参数,即使对象是future
也会返回true
。 相反,明确测试Future
案例:
Future<T> logValue<T>(FutureOr<T> value) async {
if (value is Future<T>) {
var result = await value;
print(result);
return result;
} else {
print(value);
return value as T;
}
}
错误用法示例:
Future<T> logValue<T>(FutureOr<T> value) async {
if (value is T) {
print(value);
return value;
} else {
var result = await value;
print(result);
return result;
}
}
在这个糟糕的示例中,如果您向它传递一个Future
,它会错误地将其视为一个简单的同步值。