Mohamed Mohamed
Mohamed Mohamed

Reputation: 4295

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

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'

Upvotes: 19

Views: 2495

Answers (3)

Jesther
Jesther

Reputation: 1

it will affect your IDE/analyses performance

Upvotes: 0

Peter Ewanfo
Peter Ewanfo

Reputation: 154

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

Upvotes: 6

Ahmed Khattab
Ahmed Khattab

Reputation: 2799

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.

Upvotes: 2

Related Questions