Kernel
When you createVelvetApp
, you are creating a VelvetKernel
instance. The kernel is the heart of your application. It is responsible for managing the application lifecycle, booting plugins, and handling errors.
Core Concepts
On initialization, the kernel sets up the container
and registers some core services required for the application to function like the manager, ConfigManager
and PluginManager
, and some classes like the Logger
and EventBus
.
Also register it self as as singleton in the container.
Customize your velvet app
The entire configuration of your Velvet app is done in the main.dart file chaining the methods of the VelvetKernel
instance and writing some callbacks.
This is the first look of a Velvet app:
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp().run();
}
Add a plugin
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..withPlugins((pluginManager) {
pluginManager.add(MyPlugin());
})
..run();
}
Add a plugin manager observer
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..withPlugins((pluginManager) {
pluginManager
..addManagerObserver(MyPluginManagerObserver())
})
..run();
}
Add a plugin observer
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..withPlugins((pluginManager) {
pluginManager
..addPluginObserver(MyPluginObserver())
})
..run();
}
Add a configuration
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..withConfig((configManager) {
configManager.register<MyConfigContract>(MyConfig());
})
..run();
}
Add a register callback
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..withRegister((container) {
container.register<MyServiceContract>((container) => MyService());
})
..run();
}
Add a boot callback
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..withBoot((container) {
final myService = container.get<MyServiceContract>();
myService.boot();
})
..run();
}
Add riverpod override
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..addRiverpodOverride(exampleProvider.overrideWith((ref) => 'Hello, Velvet!'))
..run();
}
Add riverpod observer
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..addRiverpodObserver(MyRiverpodObserver())
..run();
}
Override the App Widget
WARNING
This feature is not yet available.
Override the Error Widget
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..usingError(AppKernelErrorWidget())
..run();
}
Override the Loading Widget
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..usingLoading(AppKernelErrorWidget())
..run();
}
Opt out from the env loading
import 'package:velvet_framework/velvet_framework.dart';
void main() {
createVelvetApp()
..optOutFromEnvLoading()
..run();
}
Extending the Kernel
To provide more methods to the kernel, you can create extensions on VelvetKernelContract
and use it in the main function.
Creating an extension could be useful to clean up the main function and make it more readable.
This helps to keep the main function clean and easy to read.
extension MyPluginInstaller on VelvetKernelContract {
void installMyPlugin() {
withPlugins((pluginManager) {
pluginManager.add(MyPlugin());
});
}
}
Then, use it in the main function:
void main() {
createVelvetApp()
..installMyPlugin()
..run();
}