Search⌘ K
AI Features

Dart Libraries #1

Explore how to use Dart libraries to write modular code by applying prefixes, importing specific APIs with show and hide keywords, and implementing deferred loading. Understand how to handle library name conflicts and optimize resource usage through asynchronous library loading. This lesson helps you confidently manage and use Dart libraries in your applications.

Introduction

This lesson is an introduction to libraries in Dart/Flutter. A library is a reusable module for frequently used programs/code. It helps to write a modular codebase. In Dart, each app is a library.

The lesson about libraries is divided into two lessons.

Dart Libraries #1: The first lesson covers how to use Dart libraries including the following:

  • Using Prefix for library
  • Importing specific APIs
  • Defer/delay loading library

Dart Libraries #2: The second lesson instructs on understanding the following directives:

  • The part directive
  • The library directive
  • The export directive

Setup

Let’s create two libraries lib1 and lib2 for demonstration.

lib1

This library has APIs to calculate the sum and difference of two integers.

  • Addition API: The int addition(int a, int b) calculates the sum of two given integers and returns an integer.

  • Subtraction API: The int subtraction(int a, int b) calculates the difference of two given integers and returns an integer.

  • Internal Method-int _add(int a, int b): It is an internal function/method that performs the real addition. This method is used by the addition API. Identifiers with the underscore (_) prefix are ...