Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...