I am trying to implement stacked architecture by following this video uploaded by FilledStacks. I encountered a problem while using injectable pub package https://pub.dev/packages/injectable
My locator.dart class code is as follow
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
final locator = GetIt.instance;
@injectableInit
void setupLocator() => $initGetIt(locator);
on $initGetIt(locator) I'm getting this error
The function '$initGetIt' isn't defined.
Try importing the library that defines '$initGetIt', correcting the name to the name of an existing function, or defining a function named
Even though I am implementing package the way it is shown in the package documentation
https://www.youtube.com/watch?v=DO8le1W_HqQ&t=914s
Add injectable_generator package in pubspec.yaml below dev_dependencies:
Your pubspec.yaml file should look like this:
dependencies:
...other dependencies
get_it: ^7.2.0
injectable: ^2.1.0 # Version 2
json_annotation: ^4.7.0
dev_dependencies:
...other dependencies
build_runner: ^2.3.3
injectable_generator: ^2.1.3 # Version 2
json_serializable: ^6.5.4
And your code should look like this:
For injectable version 1:
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
GetIt getIt = GetIt.instance;
@InjectableInit(preferRelativeImports: false)
void setup() => $initGetIt(getIt);
For injectable version 2:
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
GetIt getIt = GetIt.instance;
@InjectableInit(preferRelativeImports: false)
void setup() => getIt.init();
Run flutter pub run build_runner build --delete-conflicting-outputs and fix
Import the injector.config.dart file:
import 'package:<app_name/your_injector_file_path>/injector.config.dart';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With