Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any difference between importing whole file and importing only class with show in Dart?

Tags:

flutter

dart

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.

like image 518
Augustin R Avatar asked Sep 10 '19 08:09

Augustin R


People also ask

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

import show : This is used to import part of the library, show only import one name of the library. import hide : This is another one which is the opposite of the show , hide import all names except the name specified in the hide .

What is as after import?

Method that is used in connection with the transport of an object into a different system. The after-import method is called in the target system after the object has been imported. The after-import method is object specific and, therefore, you have to rewrite it for every object type.

How do I import library in dart?

When importing a library file from another package, use the the package: directive to specify the URI of that file. import 'package:utilities/utilities. dart'; When importing a library file from your own package, use a relative path when both files are inside of lib, or when both files are outside of lib.


2 Answers

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
like image 150
CopsOnRoad Avatar answered Nov 02 '22 23:11

CopsOnRoad


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.

like image 38
Peter Haddad Avatar answered Nov 02 '22 23:11

Peter Haddad