dep_gen 1.0.3 dep_gen: ^1.0.3 copied to clipboard
Generates methods for creating instances of classes with automatic dependency substitution.
Generates methods for creating instances of classes with automatic dependency substitution.
Features #
This package allows you to generate special methods that create instances of classes with automatic dependency substitution. These methods will be useful when building widgets and will avoid constantly specifying dependencies in some classes. Basically, the package is focused on substituting arguments having the type described in the same project.
Installation #
If you are using creating a Flutter project:
dart pub add dep_gen
Usage #
Step 1 Describe map of available dependencies and their communications:
class CartRepository {}
class UserRepository {}
class Environment {
static Map<Type, Object> prepare() =>
{
CartRepository: CartRepository(),
UserRepository: UserRepository(),
};
}
Step 2 Integrate Dependencies
widget on top of other widgets. The code of this widget and
its type name will be generated later.
class Example extends StatelessWidget {
const Example({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Dependencies(
environment: Environment.prepare(),
child: Application(),
);
}
}
Step 3 Describe the class for whose constructors build methods will be generated.
@DepGen() is an annotation indicating which class to perform code generation for.
@DepArg() is an annotation indicating which arguments will be automatically substituted by the code generator.
@DepGen()
class SomeBloc {
Bloc({
@DepArg() required this.cartRepository,
@DepArg() required this.userRepository,
});
final CartRepository cartRepository;
final UserRepository userRepository;
}
The DepArg annotation can only be used for named and optional constructor arguments.
Step 4 Run code generator from project directory.
flutter pub run dep_gen:generate -o Dependencies
By default, the generated class is called Di, it is generated in the builder.dep_gen.dart file and is located in the lib/generated folder. But these parameters can be configured using command line parameters. You can view the description of the settings by calling the code
flutter pub run dep_gen:generate -h
Step 4 If you do not have any syntax errors in the code, a file will be generated in which the constructors of all designated classes will be collected with automatic dependency substitution.
You can use the resulting code as follows:
class Application extends StatelessWidget {
const Application({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider<SomeBloc>(
create: (context) => Dependencies.of(context).buildSomeBloc(),
child: SomeBlocConsumer(),
);
}
}
Arguments combination #
You can combine positional, named, and optional arguments together with automatically substituted arguments. For example, for a constructor with such a set of arguments:
@DepGen()
class UserDetails {
const UserDetails(final int id,
String? username, {
@DepArg() required this.api,
int? userGroup,
@DepArg() required this.cartRepository,
@DepArg() StoreRepository storeRepository,
}) :_storeRepository = storeRepository;
final Api api;
final CartRepository cartRepository;
final StoreRepository _storeRepository;
}
the build method will be generated:
class Application extends StatelessWidget {
const Application({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider<SomeBloc>(
create: (context) =>
Dependencies.of(context).buildUserDetails(
id, username, userGroup: userGroup,
),
child: SomeBlocConsumer(),
);
}
}