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
如果你有一个图书馆my_library通常是用来当你导入库是相互矛盾的类,例如。镖”,它包含一个名为类和Stream
你也想用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
}