flutter_service_provider 1.1.2
flutter_service_provider: ^1.1.2 copied to clipboard
Useful widgets for dart_service_provider package, easy to get services from service provider in the widget tree.
flutter_service_provider #
Useful widgets for dart_service_provider package.
Features #
Widgets:
Services<T>
build service provider.ScopedServices<T>
create a service scope.ServiceConsumer<T>
pass the service provider to your widget builder.ServiceInheritedWidget<T>
sharesIServiceProvider
instance to its child widgets.
Get the IServiceProvider
instance:
Services.of<T>(context)
Get theIServiceProvider
instance from the nearestServiceInheritedWidget<T>
, notes thatServices<T>
andScopedServices<T>
will createServiceInheritedWidget<T>
.ServiceConsumer<T>(builder: (context, provider){})
TheIServiceProvider
instance from the nearestServiceInheritedWidget<T>
will pass into thebuilder
argument.- Routes and scoped services
If you want to get the scoped
IServiceProvider
that created by other route, use theServiceInheritedWidget<T>
to wrap the widgets of the new route.
Single root service provider #
This is recommended and it's easy to use. Most application should only use a single root service provider.
Do not specify the generic type T
or use the same T
when you use the widgets above.
Services();
ScopedServices();
ServiceConsumer();
Multiple root service providers #
Very few applications require more than one root service provider.
Use the generic type parameter 'T' to distinguish between different root service providers.
Each root service provider is independent of the others, and if your application needs to use
multiple isolated root service providers, specify a different generic type parameter 'T' for each
root service provider.
The generic type parameter 'T' is used to distinguish between different root service providers.
Example:
-
RootServiceProvider1
When your useRootServiceProvider1
as theT
, It will be resolve services fromServices<RootServiceProvider1>
or its service scope.class RootServiceProvider1 {} Services<RootServiceProvider1>(); ScopedServices<RootServiceProvider1>(); ServiceConsumer<RootServiceProvider1>();
Use
RootServiceProvider1
asT
to get theIServiceProvider
instance:final serviceProvider = Services.of<RootServiceProvider1>(context);
-
RootServiceProvider2 When your use
RootServiceProvider2
as theT
, It will be resolve services fromServices<RootServiceProvider2>
or its service scope.class RootServiceProvider2 {} Services<RootServiceProvider2>(); ScopedServices<RootServiceProvider2>(); ServiceConsumer<RootServiceProvider2>();
Use
RootServiceProvider2
asT
to get theIServiceProvider
instance:final serviceProvider = Services.of<RootServiceProvider2>(context);
Getting started #
You can use Services.of<T>
static method to get IServiceProvider
once you use the widgets above
to wrap
your widgets.
-
Single root service provider in your application.
class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { /// Get the IServiceProvider instance. final IServiceProvider serviceProvider = Services.of(context); /// ... return Text(serviceProvider .getRequiredService<MyService>() .applicationName); } }
-
Multiple root service providers in your application.
class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { /// Get the IServiceProvider from the type of Root1 final IServiceProvider serviceProvider = Services.of<Root1>(context); /// Get the IServiceProvider from the type of Root2 final IServiceProvider serviceProvider2 = Services.of<Root2>(context); /// ... return Text(serviceProvider .getRequiredService<MyService>() .applicationName); } }
Usage #
Here's a short example. For a full example, check out example.
void main() {
runApp(
/// Use Services<T> as root
Services(
serviceConfig: (services) => services.addApplicationServices(),
builder: (context, _) => const MyApp(),
),
);
}
extension MyAppServiceCollectionExtensions on IServiceCollection {
void addApplicationServices() {
this
..addLogging(config: (b) => b.useLogger())
..addEnvironment(Environment(name: Environments.development));
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final logger = Services.of(context).getRequiredLogger<MyApp>();
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"),
);
}
}
Additional information #
If you have any issues or suggests please redirect to repo or send an email to me.