import语句中“show”和“as”之间有什么区别?

时间:2013-11-01 08:14:40

标签: dart dart-editor

导入语句中showas之间有什么区别?

例如,

之间有什么区别
import 'dart:convert' show JSON;

import 'package:google_maps/google_maps.dart' as GoogleMap;

我何时使用show,何时应使用as

如果我切换到show GoogleMap,所有对GoogleMap的引用(例如GoogleMap.LatLng)都会被报告为未定义。

3 个答案:

答案 0 :(得分:57)

asshow是两个不同的概念。

使用as,您可以为导入的库指定名称。通常这样做是为了防止库在您拥有大量全局函数时污染您的命名空间。如果您使用as,则可以按照示例中的方式访问所有库的所有函数和类:GoogleMap.LatLng

使用show(和hide),您可以选择要在应用中显示的特定类。对于你的例子,它将是:

import 'package:google_maps/google_maps.dart' show LatLng;

通过这个,您可以访问LatLng,但该库中没有任何其他内容。与此相反的是:

import 'package:google_maps/google_maps.dart' hide LatLng;

使用此功能,您可以访问该库中除LatLng以外的所有内容。

如果要使用具有相同名称的多个类,则需要使用as。您也可以将两种方法结合起来:

import 'package:google_maps/google_maps.dart' as GoogleMap show LatLng;

答案 1 :(得分:10)

show案例:

import 'dart:async' show Stream;

这样您只能从Stream导入dart:async类,因此如果您尝试使用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.dart'它包含一个名为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,我想当我们知道我们只需要一个特定的类时就会使用它。当导入的库中存在冲突类时也可以使用。让我们在您自己的图书馆中说你有一个名为CustomStreamStream的课程,你也想使用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
}

答案 2 :(得分:1)

与库导入语句一起使用的

asshow关键字。这两个关键字与import关键字是可选的,但是使用这些关键字可以提供有关库导入的便利和其他信息。


show

show限制了只能访问该库的特定类。

import 'dart:convert' show JSON;

dart:convert库上方包含超过5种类型的转换器。 (其中有ascii,Base64,Latin1,Utf8和json)。

但是使用show关键字,您将为应用程序源文件提供仅访问该JSON转换器类的权限。

警告! :-如果您尝试访问任何其他转换器(例如ascii,Base64或Latin1),则会出现异常。 因为使用show关键字,您限制了只能访问该库api中的Json类。

因此,如果您的源文件要访问该库中的所有类,则无法为该库导入定义show关键字。


as

为库成员提供其他名称空间。

as关键字通常在包含许多全局函数的库中使用。

  • 您将通过使用类名和来访问库的静态成员。 (点运算符)。 例如:-ClassName.staticFun()

  • 此外,您还将使用对象名称和来访问实例方法和变量。 (点运算符),例如:-obj.instanceFunc()

  • 并且库源文件也可以具有全局功能。我们将以他们的名字访问他们,而无需任何家长身份。例如:-func()

因此,当我们在源文件中访问不同库的全局函数时,我们没有办法将全局函数单独标识为不同库的独立函数。

但是使用as关键字,我们可以在访问该库的全局函数之前添加名称空间。

请参见以下示例,以了解as关键字的实际好处。?

import 'package:http/http.dart' as http;

http库包含许多全局函数。下面显示了http库中的全局函数列表。

List of http library global functions

  • 访问上述http库全局函数而没有 http名称空间。(import 'package:http/http.dart';

     eg:- 
       1.  get("url")
    
       2.  post("url")
    
  • 使用 http名称空间访问上述http库的全局函数。 (import 'package:http/http.dart'as http;

     eg:- 
       1.  http.get("url")
    
       2.  http.post("url")
    

因此,使用as关键字可以轻松识别与源文件的全局函数分开的其他库的全局函数。

相关问题