Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm facing a problem in my Flutter project where I'm trying to generate the $initGetIt function using Injectable, but it's not being generated as expected. Here are the details of the issue:

  • I've checked my pubspec.yaml, and the dependencies for get_it and injectable seem to be correct. Everything is updated.
  • I've run flutter pub get and flutter pub run build_runner watch --delete-conflicting-outputs, but the $initGetIt function is still not generated.
  • Here's my injection.dart and injection.config.dart files [include code snippets here].

I'd appreciate any help or insights on what might be causing this issue and how to fix it.

What I really do to fix, but won't succeed:
**1. Fix pubspec.yaml **

**pubspec.yaml**
YAML
environment:
  sdk: '>=3.1.0 <4.0.0'

dependencies:
  bloc: ^8.1.2
  cupertino_icons: ^1.0.2
  dartz: ^0.10.1
  dio: ^5.3.3
  flutter:
    sdk: flutter
  flutter_bloc: ^8.1.3
  freezed: ^2.4.3
  freezed_annotation: ^2.4.1
  get_it: ^7.6.4
  injectable: ^2.3.2
  json_annotation: ^4.8.1
  json_serializable: ^6.7.1
  retrofit: ^4.0.2

dev_dependencies:
  build_runner: ^2.4.6
  flutter_lints: ^2.0.0
  flutter_test:
    sdk: flutter
  injectable_generator: ^2.4.1
  retrofit_generator: ^8.0.0`

**2. removing include package on analysis.option.yaml**

# **Injection.dart**
Dart
import 'package:fadvanced_gunawan3_4/injection.config.dart';
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';

final getIt = GetIt.instance;

@injectableInit
void configureInjection(String env) {
  $initGetIt(getIt, environment: env);
}

# **Injection.config.dart**
Dart
`// GENERATED CODE - DO NOT MODIFY BY HAND

// **************************************************************************
// InjectableConfigGenerator
// **************************************************************************

// ignore_for_file: type=lint
// coverage:ignore-file

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:get_it/get_it.dart' as _i1;
import 'package:injectable/injectable.dart' as _i2;

extension GetItInjectableX on _i1.GetIt {
// initializes the registration of main-scope dependencies inside of GetIt
  _i1.GetIt init({
    String? environment,
    _i2.EnvironmentFilter? environmentFilter,
  }) {
    _i2.GetItHelper(
      this,
      environment,
      environmentFilter,
    );
    return this;
  }
}

# **THE ERROR I GOT:**

**injection.dart**
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 $initGetIt.


What I have tried:

1. Fix all pubspec.yaml dependencies version
2. Removing include package on analysis.option.yaml
Posted
Updated 19-Oct-23 4:54am
v3

1 solution

You need to run
flutter packages pub run build_runner build
after
flutter pub run build_runner watch --delete-conflicting-outputs
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900