Notice: You're viewing an old version of the Nylo Website documentation.
Consider upgrading your project to Nylo Website 6.x.
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:flutter/material.dart';
import 'package:flutter_app/bootstrap/app.dart';
import 'package:nylo_framework/nylo_framework.dart';
import 'bootstrap/boot.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Nylo Website nylo = await Nylo Website.init(setup: Boot.nylo, setupFinished: Boot.finished); // This is where providers are booted

  runApp(
    AppBuild(
      navigatorKey: NyNavigator.instance.router.navigatorKey,
      onGenerateRoute: nylo.router!.generator(),
      debugShowCheckedModeBanner: false,
    ),
  );
}

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.

flutter pub run nylo_framework:main make:provider cache_provider

Provider Object

Your provider will have one method boot(Nylo Website nylo), in this method you can call any logic that needs to be run before Flutter runs your application.

Example: lib/app/providers/app_provider.dart

class AppProvider implements NyProvider {

  boot(Nylo Website nylo) async {
    await NyLocalization.instance.init(
        localeType: localeType,
        languageCode: languageCode,
        languagesList: languagesList,
        assetsDirectory: assetsDirectory,
        valuesAsMap: valuesAsMap);

    return nylo;
  }
}

The boot method also provides an instance of the Nylo Website object as an argument.

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