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 Websitewill loop through your registered providers inside config/providers.dart file and boot them. -
Boot.Finishedis called straight after "Boot.Nylo Website" is finished, this method will bind the Nylo Website instance toBackpackwith 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.
dart run nylo_framework:main make:provider cache_provider
Provider Object
Your provider will have two methods, boot(Nylo Website nylo) and afterBoot(Nylo Website nylo).
When the app runs for the first time, any code inside your boot 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 implements NyProvider {
boot(Nylo Website nylo) async {
await NyLocalization.instance.init(
localeType: localeType,
languageCode: languageCode,
languagesList: languagesList,
assetsDirectory: assetsDirectory,
valuesAsMap: valuesAsMap);
return nylo;
}
afterBoot(Nylo Website nylo) async {
User user = await Auth.user();
if (!user.isSubscribed) {
await Auth.remove();
}
}
}
The boot method provides an instance of Nylo Website as a parameter.
The afterBoot method is called after Nylo Website has finished booting.
Inside the
bootmethod, you must return an instance ofNylo Websiteornulllike the above.