16

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

Faizan Kamal
  • 1,732
  • 3
  • 27
  • 56

4 Answers4

17

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';
John Alexander Betts
  • 4,718
  • 8
  • 47
  • 72
  • 5
    The last line with the import is crucial, I wonder why it is not mentioned in the official documentation – afrish Jan 09 '23 at 11:54
11
  1. Add in pubspec.yaml in dev_dependencies: injectable_generator:
  2. Run flutter packages pub run build_runner build
  3. Import $initGetIt from generated file
jazz2000
  • 121
  • 3
  • 1
    I would also suggest to run "flutter pub run build_runner watch --delete-conflicting-outputs" instead of the second step. It will make sure you dont need to be re-running this second step multiple times after editing files. – Geraldo Neto May 21 '22 at 20:53
3

If you are using injectable 2.1.0 or above use below:

@InjectableInit(asExtension: false)
FutureOr<GetIt> _init(GetIt getIt) => init(getIt);

then run the build_runner:

flutter pub run build_runner build --delete-conflicting-outputs
Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75
0

After succesfully run on terminal

flutter packages pub run build_runner build

Import the generated file .config.dart into your file

import 'locator.config.dart';

where my file with named locator, so the generated file called locator.config.dart