DiCore mixin
Mixin for StatefulWidget States that provides easy access to dependency injection.
This mixin automatically initializes the DireContainer with discovered dependencies and provides convenient methods to resolve dependencies without manual container management.
Important: To register generated dependencies, you must import your generated
.dire_di.dart file and call registerGeneratedDependencies() manually in your app initialization:
// main.dart
import 'package:flutter/material.dart';
import 'package:dire_di/dire_di.dart';
import 'app_module.dire_di.dart'; // Your generated file
void main() async {
// Pre-initialize DI container with dependency registration
await DiCore.initialize((container) => container.registerGeneratedDependencies());
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with DiCore {
@override
void initState() {
super.initState();
// Dependencies are already registered via the callback in main()
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
Or use async pattern in individual widgets:
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> with DiCore {
UserService? userService;
@override
void initState() {
super.initState();
_initializeDependencies();
}
void _initializeDependencies() async {
userService = await getAsync<UserService>();
setState(() {}); // Trigger rebuild with loaded dependencies
}
@override
Widget build(BuildContext context) {
if (userService == null) {
return CircularProgressIndicator(); // Loading state
}
return Text('User: ${userService!.getCurrentUser()}');
}
}
Properties
-
container
→ Future<
DireContainer> -
Gets the global DireContainer instance, initializing it if necessary.
This is called automatically when using the get
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
get<
T extends Object> ([String? qualifier]) → T - Get a dependency of type T from the container.
-
getAll<
T extends Object> () → List< T> - Get all dependencies of type T from the container.
-
getAllAsync<
T extends Object> () → Future< List< T> > - Asynchronously get all dependencies of type T from the container. This method automatically initializes the container if needed.
-
getAsync<
T extends Object> ([String? qualifier]) → Future< T> - Asynchronously get a dependency of type T from the container. This method automatically initializes the container if needed.
-
has<
T extends Object> ([String? qualifier]) → bool - Check if a dependency of type T is registered in the container.
-
hasAsync<
T extends Object> ([String? qualifier]) → Future< bool> - Asynchronously check if a dependency of type T is registered in the container. This method automatically initializes the container if needed.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
register<
T extends Object> (T factory(), {String? name, ScopeType scope = ScopeType.singleton}) → Future< void> - Register a dependency manually. This is useful for registering dependencies that aren't annotated.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- currentContainer → DireContainer
-
no setter
Static Methods
-
initialize(
[void registerCallback(DireContainer)?]) → Future< void> - Manually initialize the DI container. Call this in your app's main() method if you want to pre-initialize the container.
-
reset(
) → void - Reset the container (useful for testing).