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
}