Mahdi Zakizadeh
Mahdi Zakizadeh

Reputation: 645

Is there any benefit to use `src` sub-folder inside `lib` folder

When I'm trying to develop a flutter app (not a package, etc), Is there any benefit to create all application files and folders in src sub-folder inside lib folder?

Upvotes: 18

Views: 5874

Answers (3)

Yuriy N.
Yuriy N.

Reputation: 6127

Using lib/src actually makes sense only for package writing. We put everything that can be imported by the user of the package in the lib and all private libraries inside src.

On the other side when we are used to such a convention while writing packages why not to continue use it when writing apps?

The only confusing thing is a main.dart file. The package does not have it, but the application does. Main.dart is obviously a source file, but should be placed in the lib folder.

Considering the above it is kinda should be a matter of developer/team choice.

I do it with src:

     lib
        /main.dart
        /src
            /lib_1.dart
            /lib_2.dart

Upvotes: 1

RSF
RSF

Reputation: 306

This question is tagged as Flutter but should probably also have the Dart flag as Dart is the language used by Flutter. According to Dart Organising a library Package, the convention of putting code under lib/src is called a "convention" and the purpose is to separate the library's public code from private implementation code.

There is a second reason further down that states:

Tip for web apps: For the best performance when developing with dartdevc, put implementation files under /lib/src, instead of elsewhere under /lib. Also, avoid imports of package:package_name/src/....

As the dartdevc compiler is used by flutter at development time in debug mode (see Flutter Debug Build mode), there does appear to be a design-time speed advantage with hot reload if you use the lib/src structure rather than just lib. Please note a different compiler is used for release - dart2js - so there is no advantage in this scenario (see the same link above).

A similar historical discussion on the above points can be found on the dart language github here: Recommendations for /lib/src vs. /lib

Upvotes: 19

Son of Stackoverflow
Son of Stackoverflow

Reputation: 1679

Using directories is just a way to organize your resources/files.

In the current scenario it is just to separate source files from the rest.

The main benefit of doing so is that if you open your project after a month/year for some reason then those directory names will help you to recollect things faster.

Other benefits include:

  1. Your team-mates (if any) shall be able to understand your project structure faster. Imagine if you just piled up everything in just one directory how difficult things could have got for them.
  2. Duplicate files can be stored within the same lib file.

Upvotes: -1

Related Questions