A multiple-file library – the export statement

The preceding approach is not the ideal way of splitting a Dart library, as already mentioned. This is because the syntax of the part statement is likely to change in future versions. Additionally, you may have found it a little overdone and difficult to use if you just want to control the visibility of library members.

We can choose to simply not create the library parts and just split the library into small individual libraries. For the previous examples, this would result in some important changes during implementation.

We have the previous parts as three individual libraries: person_library, programmer, and student. Although related, they behave as individual libraries and do not know anything except the public members of each other:

// person library defined in person_library.dart
class Person {
String firstName;
String lastName;
final PersonType type;

Person({this.firstName, this.lastName, this.type});

String toString() => "($type): $firstName $lastName";
}

enum PersonType { student, employee }

The person library does not need the library identifier in this case. 

The programmer library imports the person library to access its Person class:

// programmer library defined in programmer.dart

import 'person_library.dart';

class Programmer extends Person {
Programmer({firstName, lastName})
: super(firstName: firstName, lastName: lastName, type: PersonType.employee);
}

In the same way, the student library imports the person library:

// student library defined in student.dart

import 'person_library.dart';

class Student extends Person {
Student({firstName, lastName})
: super(
firstName: firstName,
lastName: lastName,
type: PersonType.student,
);
}

You can see the following from the preceding code:

  • The programmer and student libraries need to import the person library to extend it.
  • Additionally, the type property from the Person class was made public by removing the _ prefix. This means that it can be accessed by the other libraries. As the type property, in this case, is not intended to change and it is initialized in the constructor, we have made it final as well.

Let's take a look at the library customer, as follows:

import 'person_lib/programmer.dart';
import 'person_lib/student.dart';

main() {
// we can access the Programmer class as it is part of the person_library
Programmer programmer = Programmer(firstName: "Dean", lastName: "Pugh");
Student student = Student(firstName: "Dilo", lastName: "Pugh");

print(programmer);
print(student);
}

The person library customer will have a small change, as now the library is split into multiple parts, so we will need to import each library we want to use individually. 

This is not a big deal when talking about small libraries, but try to think about a more complex library structure, where importing all of the interrelated libraries individually would add difficulty to its usage.

This is where the export statement comes in. Here, we can select the main library file and, from there, export all of the smaller libraries related to it. In this way, the customer only needs to import a single library and all of the smaller libraries will be available alongside it.

In our example, the best choice for using this could be the person library:

export 'programmer.dart';
export 'student.dart';

class Person { ... }

enum PersonType { ... }

In this way, the library customer would be as follows:

import 'person_lib/person_library.dart';

main() {
// we can access the Programmer and Student class as they are exported
// from the person_library
Programmer programmer = Programmer(firstName: "Dean", lastName: "Pugh");
Student student = Student(firstName: "Dilo", lastName: "Pugh");

print(programmer);
print(student);
}

Notice that only the import statement changes. We can use the classes from the small libraries normally as they are exported from person_library.

After gaining an understanding of the Dart library concept, we can now examine how to combine these pieces of code into something shareable and reusable: the Dart package.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.243