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

nick picture nick · Nov 1, 2013 · Viewed 19.2k times · Source

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

For example, what's the difference between

import 'dart:convert' show JSON;

and

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

When do I use show and when should I use as?

If I switch to show GoogleMap all references to GoogleMap (e.g. GoogleMap.LatLng) objects are reported as undefined.

Answer

Dennis Kaselow picture Dennis Kaselow · Nov 1, 2013

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;