show案例:
import 'dart:async' show Stream;
这样你只能从dart:async 导入Stream 类,所以如果你尝试使用dart:async 中的另一个类而不是Stream 会抛出错误。
void main() {
List data = [1, 2, 3];
Stream stream = new Stream.fromIterable(data); // doable
StreamController controller = new StreamController(); // not doable
// because you only show Stream
}
as案例:
import 'dart:async' as async;
这样你就可以从dart:async 导入所有类并用async 关键字命名它。
void main() {
async.StreamController controller = new async.StreamController(); // doable
List data = [1, 2, 3];
Stream stream = new Stream.fromIterable(data); // not doable
// because you namespaced it with 'async'
}
as 通常在您导入的库中存在冲突类时使用,例如,如果您有一个包含名为 Stream 的类的库“my_library.dart”,并且您还想使用来自的 Stream 类dart:async 然后:
import 'dart:async';
import 'my_library.dart';
void main() {
Stream stream = new Stream.fromIterable([1, 2]);
}
这样,我们不知道这个 Stream 类是来自异步库还是您自己的库。我们必须使用as:
import 'dart:async';
import 'my_library.dart' as myLib;
void main() {
Stream stream = new Stream.fromIterable([1, 2]); // from async
myLib.Stream myCustomStream = new myLib.Stream(); // from your library
}
对于show,我想这是在我们知道我们只需要一个特定的类时使用的。当导入的库中存在冲突的类时也可以使用。假设在您自己的库中,您有一个名为 CustomStream 和 Stream 的类,并且您还想使用 dart:async,但在这种情况下,您只需要您自己的库中的 CustomStream。
import 'dart:async';
import 'my_library.dart';
void main() {
Stream stream = new Stream.fromIterable([1, 2]); // not doable
// we don't know whether Stream
// is from async lib ir your own
CustomStream customStream = new CustomStream();// doable
}
一些解决方法:
import 'dart:async';
import 'my_library.dart' show CustomStream;
void main() {
Stream stream = new Stream.fromIterable([1, 2]); // doable, since we only import Stream
// async lib
CustomStream customStream = new CustomStream();// doable
}