Providers
Introduction to Providers
In Nylo, 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.
Deep dive
import 'package:nylo_framework/nylo_framework.dart';
import 'bootstrap/boot.dart';
/// Nylo - Framework for Flutter Developers
/// Docs: https://nylo.dev/docs/6.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
will loop through your registered providers inside config/providers.dart file and boot them. -
Boot.Finished
is called straight after "Boot.Nylo" is finished, this method will bind the Nylo instance toBackpack
with the value 'nylo'.
E.g. Backpack.instance.read('nylo'); // Nylo 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 nylo)
and afterBoot(Nylo 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 nylo) async {
await NyLocalization.instance.init(
localeType: localeType,
languageCode: languageCode,
languagesList: languagesList,
assetsDirectory: assetsDirectory,
valuesAsMap: valuesAsMap);
return nylo;
}
afterBoot(Nylo nylo) async {
User user = await Auth.user();
if (!user.isSubscribed) {
await Auth.remove();
}
}
}
The boot
method provides an instance of Nylo as a parameter.
The afterBoot
method is called after Nylo has finished booting.
Inside the
boot
method, you must return an instance ofNylo
ornull
like the above.