Home > Mobile >  Import and Exports files in Flutter Project
Import and Exports files in Flutter Project

Time:10-05

guys!

When developing hybrid apps using flutter, sometimes the imports section takes too many lines of code. As a solution to this, I saw in a certain course the practice of creating a file that only has exports, and then use this file to import all the exports file specified. As in the example:

myExportFile.dart

export 'package:flutter/myCustomWidgetBlack.dart';
export 'package:flutter/myCustomWidgetWhite.dart';
export 'package:flutter/myCustomWidgetRed.dart';

In practice, insted calling the import to each custom widget, a I only imports myExportFile.dart and then, i'll have all the necessarie import to use them.

This is a good practice? Has some negative point by doing this?

CodePudding user response:

As long as you export related files together, it's ok. The developer should be able to identify which import statement is importing which symbol. Also, this hides the internals of a package. Imagine a scenario where you want to use a package usually you need to import one file. If there was no export mechanism you'll have probably many files import just to use one package. What can go wrong here, is having an absurd import that make the developer dig in a nested export statement and make it hard for him to navigate in the code. So, keep the export statements simple, pack related symbols/files together, and don't hide unexpected export under an unexpected import.

  • Related