Dart Libraries - Private Fields , Import, Export, Part & Part of

On day 8 of my 100-day Flutter learning journey, I will write about my understanding of Dart libraries and other topics under that heading. The words package and project, file and library will be interchanged throughout this post; whenever I use any of these words, I will be referring to the same thing.

Libraries And Files

By default, every Dart file is a standalone library. It is necessary to create a library in the lib folder that contains implementations of a package's features and functions. A unique tag is generated when a Dart file is created based on the file name and path. In order to declare or use a class inside another library, you must import the library from which it was created. Only in that way can a library gain access to another library's content.

The private fields of a library can’t be accessed by another library. This is because Dart doesn’t offer Class Private Fields like other programming languages, but rather Library Private Fields, meaning a variable that belongs to a class is only accessible within that class and its library and not by any other code that imports the library containing the class

How Dart Libraries Are Formed

  1. A Dart library can be formed from multiple files, and the files from the library won’t be libraries on their own in such instances. The files will represent and only be a part of the main library. To add the content of one library to another library to form a single library, the part and part of keywords are used. In the library you want to keep, you write the path part:‘second_library.dart’ of the library you want to add to it using the part keyword followed by a string containing the path of the library you want to add. Then you add the path part of: ‘first_library.dart’ of the main library to the library you’re exporting since it’s now an extension of the first library and not a library on itself.

    Note that;

    1. Only part of directories are allowed inside library extension files. To import a library, you have to do it in the library's main file first_library.dart and it will be available for all other files second_library.dart in the library.

    2. All extension files can access both public and private members of the entire library.

    3. When accessing a single class or method from an extension file outside the library, the entire library including every other extension file will also be included in the import.

  2. A Dart library can also contain multiple sub-libraries that are stand-alone and do not depend on the private fields and methods of other libraries. For the top library to access and auto-import all the small libraries when we import them somewhere else we use the export keyword in the main library to export every small library export 'second_library.dart'.

    So importing the big library in another file will subsequently import the rest of the libraries including the one containing the implementations you want to use.

    And you can also import only the libraries containing the implementations you need.

Some Rules To Follow When Working With Libraries

  1. Declare all your libraries containing feature implementations inside the lib folder.

  2. To access libraries in the lib folder from outside, you have to import the library using only the packages directory. import ‘package:dart_libraries/top_library/second_library_second_library.dart

  3. Don’t allow an import path to reach in and out of the library by using relative paths. The only way to achieve this is by using a package directory