flutter_service_container 0.0.3
flutter_service_container: ^0.0.3 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:
ServicesRootprovide a root service provider for its child.ServicesScopeprovide a scope service provider for its child.Servicesinherited widget shared service provider for its child.ServiceConsumerconsumes services.DeveloperLogPrinterforwards logs to the dart:developer log() API.
Get IServiceProvider instance:
Services.of(context)
Get theIServiceProviderinstance from the nearestServicesinherited widget. Notes thatServicesRootandServicesScopewill createServicesinherited widget.ServiceConsumer(builder: (context, provider){})
TheIServiceProviderinstance from the nearestServicesinherited widget will pass into theproviderparameter of thebuilder.- Routes
If you want to get the
IServiceProviderthat created or provided by other route, use theServicesinherited 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.
ServiceDescriptor<Logger> exampleLogger = ServiceDescriptor.singleton((p) => Logger("Example"));
void main() {
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.
useDeveloperLogPrinter() 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.