A concrete implementation of the todos_repository
for Flutter apps.
The code is in todos_repository/lib/
.
The tests can be run from the command line
cd flutter_architecture_samples/todos_repository
flutter test
- Provides an implementation of the data layer
- Can be a pure Dart library or a Flutter library
This library implements the todos_repository
. It uses a File Storage and Web Client (currently a Mock).
This implementation tries first to load the todos from storage, then falls back to web if none are found. It persists changes to both the file system and the web service.
Generally speaking, if a library can be a pure Dart library, it probably should be. This makes it far easier to reuse because it has far fewer dependencies and easier to test because you do not have to mock out the Flutter environment.
For example, in order to test the File Storage part of this library, we have two options:
- Make this a pure Dart Lib
- The
FileStorage
class takes agetDirectory
function that will provide the correctDirectory
for the given situation. - In Flutter, we'll pass through the
path_provider
functiongetApplicationDocumentDirectory
. - In tests, we'll pass through a function that provides a temporary directory on disk.
- The
- Make this a Flutter library
- In this case, the library will require
path_provider
directly. - In your flutter app, everything should "just work" since you're depending on the environment directly.
- In tests, you need to mock the
MethodChannel
and return a temporary directory on disk for the correct situation.
- In this case, the library will require
Overall, it's not too much more difficult to test these one way or the other. However, if you can avoid pulling in large dependencies, such as Flutter, you should. This will make it easier to use this library in other, pure Dart libs, or even in web projects, and will still function perfectly within your Flutter projects.