ZAP Architecture is a set of helper classes and libraries, that are designed to integrate with the Provider package. These libraries will reduce your development time when using Provider by handling all the boiler plate in a beautiful and efficient way.

All you have to do is to import the plugin, add Provider to your project and start building, see the usage examples below.

Usage

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

Using: Response

Simple Usage

import 'zap_architecture/zap_architecture';

void main() async {
  Response response = await _fetchData();

  if (response.success)
    print("RESPONSE RECEIVED: ${response.data}"); // RESPONSE RECEIVED: Awesome Data
  else
    print("RESPONSE RECEIVED FAILURE!!!");
}

Future<Response> _fetchData() async {

  //Wait 2 seconds 
  await Future.delayed(Duration(seconds: 2));
  
  //Return data as success.
  return Response.success("Awesome Data");
}

Advanced Usage (Generics)

The Response class leverages the Generics to make everything more static and compile safe. This also demonstrates how the developer can easily check if the response is successful, the attached data, and the errorMessage.
Check the code below on how to use it in the same example as above.

import 'zap_architecture/zap_architecture';

void main() async {
  Response<String> response = await _fetchData();

  if (response.success) {
      print("RESPONSE RECEIVED: ${response.data}"); // RESPONSE RECEIVED: Awesome Data
      print("RESPONSE DATA TYPE: ${response.data.runtimeType}"); // RESPONSE DATA TYPE: String
    } else {
      print("RESPONSE RECEIVED FAILURE: ${response.friendlyErrorMessage}"); // RESPONSE RECEIVED FAILURE: Try again, you have a 50% chance....
      print("RESPONSE RECEIVED FAILURE: DEBUG INFO: ${response.errorMessage}"); // RESPONSE RECEIVED FAILURE: DEBUG INFO: null.
    }
}

//50% chance of receiving successful response.
Future<Response<String>> _fetchData() async {
  int rand = await Future.delayed(
    Duration(seconds: 2),
    () => Random().nextInt(50),
  );

  bool randomSuccessful = rand == 50;

  if (randomSuccessful)
    //return Response.success(9); This will not work, your IDE will not let you send an int value where String is needed.
    return Response.success("Awesome Data");
  else
    return Response.error(friendlyErrorMessage: "Try again, you have a 50% chance of getting a successful response.");
}

Using: PlatformsMixin

PlatformsMixin is a small utility mixin which lets you check if the app is being run on Android or iOS. This is added to a Widget and then it's provided values can be used anywhere within that widget.

Use inside StatelessWidget

import 'package:flutter/material.dart';
import 'package:zap_architecture/zap_architecture.dart';

class MyWidget extends StatelessWidget with PlatformsMixin {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text("App is running in iOS: $isIOS"), //App is running in iOS: true / false
        Text("App is running in Android: $isAndroid"), //App is running in Android: true / false
      ],
    );
  }
}
}

Use inside StatefulWidget

import 'package:flutter/material.dart';
import 'package:zap_architecture/zap_architecture.dart';

class MyWidget extends StatefulWidget with PlatformsMixin {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    
    print("App is running in iOS" + widget.isIOS.toString());
    print("App is running in Android" + widget.isAndroid.toString());

    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text("App is running in iOS: ${widget.isIOS}"), // App is running in iOS: true / false
        Text("App is running in Android: ${widget.isAndroid}"), // App is running in Android: true / false
      ],
    );
  }
}

Libraries

zap_architecture