flutter_service_container 0.0.10
flutter_service_container: ^0.0.10 copied to clipboard
Useful widgets for service_container package that support share service provider instances.
flutter_service_container #
Useful widgets for service_container package.
Features #
Widgets:
ServicesRoot
provide a root service provider for its child.ServicesScope
provide a scope service provider for its child.Services
inherited widget shared service provider for its child.ServiceConsumer
consumes services.DeveloperLogPrinter
forwards logs to the dart:developer log() API.
Get IServiceProvider
instance:
Services.of(context)
Get theIServiceProvider
instance from the nearestServices
inherited widget. Notes thatServicesRoot
andServicesScope
will createServices
inherited widget.ServiceConsumer(builder: (context, provider){})
TheIServiceProvider
instance from the nearestServices
inherited widget will pass into theprovider
parameter of thebuilder
.- Routes
If you want to get the
IServiceProvider
that created or provided by other route, use theServices
inherited widget to wrap the widgets of the new route child widget.
Getting started #
Install
flutter pub add flutter_service_container
Usage #
Here's a short example. For a full example, check out example.
// define a logger service
ServiceDescriptor<Logger> $exampleLogger = ServiceDescriptor.singleton((p) => Logger("Example"));
void main() {
// configure the container, use developer log printer
containerConfigure.useDeveloperLogPrinter();
/// Use ServicesRoot to provide a root service provider for the app.
runApp(
ServicesRoot(
printDebugLogs: true,
child: const MyApp(),
),
);
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final logger = Services.of(context).getService($exampleLogger);
logger.info("$MyApp is building");
return MaterialApp(
title: 'Flutter service Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: MyHomePage(title: "Flutter service example"),
);
}
}
Logging #
Use logging package to logging.
containerConfigure.useDeveloperLogPrinter()
extension method will override the default log
printer that defined in service_container package and
use DeveloperLogPrinter
.
DeveloperLogPrinter
forwards logs to the dart:developer log() API.