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

后端 未结 3 2114
眼角桃花
眼角桃花 2020-12-01 00:57

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

For example, what\'s the difference between

import \'da         


        
3条回答
  •  被撕碎了的回忆
    2020-12-01 01:37

    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;
    

提交回复
热议问题