Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injectable: Created file resolves imports incorrectly #12

Closed
PDDStudio opened this issue Feb 19, 2020 · 12 comments
Closed

Injectable: Created file resolves imports incorrectly #12

PDDStudio opened this issue Feb 19, 2020 · 12 comments

Comments

@PDDStudio
Copy link

Hello,

We've started using injectable in our project.
I've realized that the generated file has some problems with wrong/duplicated imports.

For example, I've my dio instance registered via registerModule & lazySingleton.

@registerModule
abstract class RegisterModules {

  @lazySingleton
  Dio get dio => Dio();

}

After generating the iconfig.dart file I can see that there are problems with resolving the correct import of dio:

// GENERATED CODE - DO NOT MODIFY BY HAND

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

<other imports>
import 'package:dio/dio.dart';
import 'package:get_it/get_it.dart';
import 'package:dio/src/dio.dart';

void $initGetIt(GetIt g, {String environment}) {
  g.registerLazySingleton<Dio>(() => Dio());
  _registerEagerSingletons(g, environment);
}

// more code

I don't know if this issue has been reported yet, but I couldn't find anything similar to this.
For now I'm ending up removing the duplicated (& wrong) import by hand.

@ResoDev
Copy link

ResoDev commented Feb 20, 2020

This also happens with Firebase. It imports .../src/something.dart although 3rd party package imports should never point to the src directory directly.

@Milad-Akarie
Copy link
Owner

Hey @ResoDev @PDDStudio,
Are you sure you're on the latest version 0.2.2?

@Milad-Akarie
Copy link
Owner

@PDDStudio You mean two import statements are generated?

@ResoDev
Copy link

ResoDev commented Feb 20, 2020

@Milad-Akarie yes, just tested it again.
Injectable imports

import 'package:firebase_auth/src/firebase_auth.dart';

which is actually an error: The imported library ''package:firebase_auth/src/firebase_auth.dart'' can't have a part-of directive. Try importing the library that the part is a part of.dart(import_of_non_library)

Here's the module:

@registerModule
abstract class FirebaseInjectableModule {
  @singleton
  GoogleSignIn get googleSignIn => GoogleSignIn();
  @singleton
  FirebaseAuth get firebaseAuth => FirebaseAuth.instance;
}

Once this gets resolved, this library will be 🔥🔥🔥💯

@Milad-Akarie
Copy link
Owner

@ResoDev I'm actually working on a better approach to handling custom initializers that will solve all the import issues and more, hopefully it will be done by this weekend so stay tuned.

@ResoDev
Copy link

ResoDev commented Feb 20, 2020

@Milad-Akarie Thank you. I hope we're going to arrive at a stable-ish version because I'd love to use this package in a tutorial series.

A bit off-topic here, but get_it 4.0.0 shouldn't disrupt the API of this lib, right? Maybe just the implementation.

@Milad-Akarie
Copy link
Owner

@ResoDev I don't think it will, I've already tried it with get_it v4 and everything worked just fine, I'll add support to v4 as soon as it's released or maybe even before.

@Milad-Akarie
Copy link
Owner

@PDDStudio @ResoDev This should be fixed in v0.2.3 check it out!
Thanks for reporting.

@PDDStudio
Copy link
Author

Sorry for the late reply, I totally lost track of this issue. We've updated to 0.3.0 today and this issue is fixed. Thanks!

@sedobrengocce
Copy link

sedobrengocce commented May 18, 2020

@Milad-Akarie
I'm using versio 0.4.0 and the problem is present again

// GENERATED CODE - DO NOT MODIFY BY HAND

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

import 'package:http/src/client.dart'; import 'package:get_it/get_it.dart';

void $initGetIt(GetIt g, {String environment}) {
final injectableModules = _$InjectableModules();
g.registerLazySingleton(() => injectableModules.httpClient);
<more generated code

}

class _$InjectableModules extends InjectableModules {}

@Milad-Akarie
Copy link
Owner

Milad-Akarie commented May 19, 2020

@sedobrengocce it's importing from the src not the library?
could you show me your setup (The module)?

@sedobrengocce
Copy link

sedobrengocce commented May 19, 2020

@Milad-Akarie sure!

import 'package:injectable/injectable.dart';

import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';

@module
abstract class InjectableModules {
  @lazySingleton
  http.Client get httpClient => http.Client();

  @lazySingleton
  @preResolve
  Future<SharedPreferences> get sharedPreferences async => await SharedPreferences.getInstance();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants