What is the difference between “show” and “as” in an import statement?

筅森魡賤 提交于 2019-11-28 05:43:45
Dennis Kaselow

as and show are two different concepts.

With as you are giving the imported library a name. It's usually done to prevent a library from polluting your namespace if it has a lot of global functions. If you use as you can access all functions and classes of said library by accessing them the way you did in your example: GoogleMap.LatLng.

With show (and hide) you can pick specific classes you want to be visible in your application. For your example it would be:

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

With this you would be able to access LatLng but nothing else from that library. The opposite of this is:

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

With this you would be able to access everything from that library except for LatLng.

If you want to use multiple classes with the same name you'd need to use as. You also can combine both approaches:

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

show case:

import 'dart:async' show Stream;

This way you only import Stream class from dart:async, so if you try to use another class from dart:async other than Stream it will throw an error.

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 case:

import 'dart:async' as async;

This way you import all class from dart:async and namespaced it with async keyword.

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 is usually used when there are conflicting classes in your imported library, for example if you have a library 'my_library.dart' that contains a class named Stream and you also want to use Stream class from dart:async and then:

import 'dart:async';
import 'my_library.dart';

void main() {
  Stream stream = new Stream.fromIterable([1, 2]);
}

This way, we don't know whether this Stream class is from async library or your own library. We have to use 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
}

For show, I guess this is used when we know we only need a specific class. Also can be used when there are conflicting classes in your imported library. Let's say in your own library you have a class named CustomStream and Stream and you also want to use dart:async, but in this case you only need CustomStream from your own library.

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
}

Some workaround:

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
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!