Advanced

Providers

Introduction to Providers

In Nylo Website, providers are booted initially from your main.dart file when your application runs. All your providers reside in /lib/app/providers/*, you can modify these files or create your providers using Metro.

Providers can be used when you need to initialize a class, package or create something before the app initially loads. I.e. the route_provider.dart class is responsible for adding all the routes to Nylo Website.

Deep dive

import 'package:nylo_framework/nylo_framework.dart';
import 'bootstrap/boot.dart';

/// Nylo - Framework for Flutter Developers
/// Docs: https://nylo.dev/docs/7.x

/// Main entry point for the application.
void main() async {
  await Nylo.init(
    setup: Boot.nylo,
    setupFinished: Boot.finished,

    // showSplashScreen: true,
    // Uncomment showSplashScreen to show the splash screen
    // File: lib/resources/widgets/splash_screen.dart
  );
}

Lifecycle

  • Boot.Nylo Website will loop through your registered providers inside config/providers.dart file and boot them.

  • Boot.Finished is called straight after "Boot.Nylo Website" is finished, this method will bind the Nylo Website instance to Backpack with the value 'nylo'.

E.g. Backpack.instance.read('nylo'); // Nylo Website instance

Create a new Provider

You can create new providers by running the below command in the terminal.

metro make:provider cache_provider

Provider Object

Your provider will have two methods, setup(Nylo nylo) and boot(Nylo nylo).

When the app runs for the first time, any code inside your setup method will be executed first. You can also manipulate the Nylo object like in the below example.

Example: lib/app/providers/app_provider.dart

class AppProvider extends NyProvider {

  @override
  Future<Nylo?> setup(Nylo nylo) async {
    await NyLocalization.instance.init(
        localeType: localeType,
        languageCode: languageCode,
        languagesList: languagesList,
        assetsDirectory: assetsDirectory,
        valuesAsMap: valuesAsMap);

    return nylo;
  }

  @override
  Future<void> boot(Nylo nylo) async {
    User user = await Auth.user();
    if (!user.isSubscribed) {
      await Auth.remove();
    }
  }
}

Lifecycle

  1. setup(Nylo nylo) - Initialize your provider. Return the Nylo instance or null.
  2. boot(Nylo nylo) - Called after all providers have finished setup. Use this for initialization that depends on other providers being ready.

Inside the setup method, you must return an instance of Nylo or null like the above.