Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Is there an effect on having a single import rather than multiple?

Tags:

dart

Basically I have a lot of widgets and services and stuff, like most people do, that I need to access throughout the app. I was wondering if have a single file with an export of every single file and then just simply importing that one file in every page/file i need to access something rather than just importing specific files that the page needs, will it slow down the app or cause any issues or increase file size, etc... or will it behave the same?

Example

login_page.dart

import '1.dart'
import '2.dart'

home_page.dart

import '2.dart'
import '3.dart'
import '9.dart'
import '10.dart'

settings_page.dart

import '1.dart'
import '2.dart'
import '9.dart'
import '10.dart'

or...

all_imports.dart:

export '1.dart'
export '2.dart'
export '3.dart'
... (up until)
export '10.dart'

in every dart file:

import 'all_imports.dart'
like image 774
Mohamed Mohamed Avatar asked Mar 22 '20 05:03

Mohamed Mohamed


2 Answers

Using 'all_imports.dart' may cause unneeded dependencies but dart knows how to handle dependencies that called but not used.

Same implementation of 'all_imports.dart' is used by flutter team on 'material.dart'

You may wish to just make simple design but when you import 'material.dart' it brings everything to the table ('about.dart', 'app.dart', 'banner.dart') and many others.

I would advise you structure your application using 'all_import.dart' pattern

like image 130
Peter Ewanfo Avatar answered Oct 16 '22 14:10

Peter Ewanfo


actually, it dosent make a difference, lets imagine this case

in login_page.dart

import '1.dart'
import '2.dart'

here you are being explicit about the dependencies of this module/file/widget, which means it only uses what it needs. which is better for maintenance and redablity of the modules dependanices.

the other case where you have all of your imports in one file

import 'all_imports.dart'

lets see what happens here:

  1. Dart executes the file all_imports.dart
  2. that file is importing every module that you have listed in that file
  3. so the import calls happend again

which means that wont affect your software's performance if you dont have an all_imports.dart file. actulally i find that this method(the all_imports.dart) will affect your program in a bad way if any.


why? lets say we have a module A that depends on both module B and module C , you would import them this way

moduleA.dart

import 'moduleB'
import 'moduleC'

  • the advantages is that the module is now explicit in its dependencies and anyone who looks at this module/file in the future will know what they are.

however

the other method where you have all of your imports in a single all_imports.dart file, will cause unneeded dependencies to be loaded for a certain module


lets have the same example above, module A depends on moduleB and moduleC and you listed them in the all_imports.dart file, it will look like this

export `moduleA'
export 'moduleB'

/// some other modules that you are exporting

and in the moduleA.dart file you import it this way

import 'all_imports.dart`

now

the module A has successfully imported moduleB and moduleC which it needs. BUT, now it has all the other dependancies that it dose not need loaded for it although it only needs moduleA and moduleB.

like image 36
Ahmed Khattab Avatar answered Oct 16 '22 15:10

Ahmed Khattab