18

Instead of writing :

import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';

which exports a dozen of files, I would like to write :

import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'
    show
        PlatformAlertDialog,
        PlatformCircularProgressIndicator,
        PlatformDialogAction,
        PlatformText,
        showPlatformDialog;

because I'm only using these components. However this is quite tidious (reminds me of Typescript's endless imports) and goes against Dart's principle of briefness.

Imports snippets in VSCode uses the first solution, but is there any notable difference, for example in terms of performance? Is there some kind of good practice? I cannot find anything in official guidelines.

Augustin R
  • 7,089
  • 3
  • 26
  • 54
  • would you like to check this https://stackoverflow.com/questions/19723063/what-is-the-difference-between-show-and-as-in-an-import-statement – chunhunghan Sep 10 '19 at 09:12
  • 1
    My question is not about the difference between `show` and `as`, I know what `show` does, I would like to know when to use it or not. – Augustin R Sep 10 '19 at 09:15

2 Answers2

16

There is no impact on performance. The reason of using show is to reduce chances of confusion when importing classes from different packages.

For instance: Let's say

abc.dart has 2 classes

class One {}

class Two {}

And xyz.dart also has 2 classes:

class One {}

class Three {}

And you are importing both package in your file

import 'abc.dart';
import 'xyz.dart';

Say, you only want to use class One from abc.dart, so when you use One it could be from abc.dart or xyz.dart. So to prevent One coming from xyz.dart you'd use:

import `xyz.dart` show Three // which means only `Three` class can be used in your current file from xyz.dart package
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
8

When you use the keyword show basically what you are saying that I only want to use this specific class from this package in your dart file, from the docs:

Importing only part of a library

If you want to use only part of a library, you can selectively import the library. For example:

// Import only foo.
import 'package:lib1/lib1.dart' show foo;

// Import all names EXCEPT foo.
import 'package:lib2/lib2.dart' hide foo;

Now if you try to use anything from this package other than foo you will get an error because you specified that you only want to use foo.

Community
  • 1
  • 1
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • 2
    Thanks for your answer. I know what `show` and `hide` do, do you know if there is some kind of performance or design related best solution between using it or not? – Augustin R Sep 10 '19 at 09:31
  • 2
    When you import the dart file, all of the symbols from that file will become usable but when you use `show` it will just limit what you can use from that file. But I doubt it will effect performance if you use it or not. (I mean you are already importing the whole file, the `show` and `hide` just limits what to use from that file). – Peter Haddad Sep 10 '19 at 09:47