ZAP Architecture is a set of simple helper objects, that lets Dart/Flutter developers to make complex applications with ease.

The core ZAP Architecture library, this, contains only 3 classes, Response, Status, Process. You can use the core library anywhere in Dart. We at Musa Usman & Co. use it for developing Flutter apps & Dart HTTP servers.

Usage

This README file will guide you how to use each class for making your Dart development way easier and way more effective.

Response (Res) 🔥

In code, Response is called Res.

Imagine calling a function, that is suppose to return a List<String> of Wallpaper URLs. But it encounters an error, so you want it to return the error message if something goes wrong but return a list of wallpaper urls if everything is good. The issue is, that you can only return a single variable from a function, this is what Response solves.

Just return a Response of type List<String> from that function. It would look like this: Res<List<String>>

Example: Fetch Wallpaper URLs

import 'zap_architecture/zap_architecture';

void main() async {
  Res<List<String>> response = await _getWallpaperURLSFirebase();

  if (response.success)
    print("Wallpapers URLs: Received successfully: ${response.data}");
  else
    print("Wallpapers URLs: FAILURE");
}

Example: Return Wallpaper URLs

import 'zap_architecture/zap_architecture';

Future<Res<List<String>>> _getWallpaperURLSFirebase() async {
  try {

    List<String> list = ["url1", "url2", "url3"];

    return Res.success(list);
  } catch (e, s) {
    //Good way to print nice error messages
    print("Firestore: GetWallpaperURLsFirebase: $e\n$s");

    return Res.error(errorMessage: e.toString());
  }
}

Status 🔥

Imagine creating a loading variable for checking if your app is loading data or not. Also creating error to check if the app should show error page or not. Also creating an errorMessage variable to check what to show in the error page.

Now when you load a list of wallpapers, you have to first set loading = true, error = false, errorMessage = null. And after the data is loaded, you have to handle all that. If an error happens, then you have to handle that separately. That means 9 lines of boilerplate on every function. What a nightmare!

Luckily, Status makes it damn easy! Just use Status.loading(), Status.error() or Status.clear(). But this gets even better if you are using Res.

You can update a Status variable from just a Res. Using Status.fromRes().

Example: Fetch Wallpaper URLs

import 'zap_architecture/zap_architecture';

Status status = Status.clear(); //Use .clear as default value
List<String> wallpapers;

void main() async {
  status = Status.loading();

  Res<List<String>> response = await _getWallpaperURLSFirebase();

  if (response.success)
    wallpapers = response.data;

  status = Status.fromResponse(response);

}

Libraries

zap_architecture