简体   繁体   中英

How to get Injectable to generate dependency injection for third party Abstract class thats using factory constructors

I am trying to register the class below with the package Injectable .

import 'dart:io';

import 'package:injectable/injectable.dart';


@injectable
class IWantToBeRegistered {
  final File file;
  IWantToBeRegistered({
    required this.file,
  });
}

I get the response below:

[WARNING] injectable_generator:injectable_config_builder on bin/service_locator.dart:
Missing dependencies in your_project/bin/service_locator.dart

[MachineCodeWriter] depends on unregistered type [File] from dart:io

Did you forget to annotate the above class(s) or their implementation with @injectable? 
or add the right environment keys?

I have attempted registering File as a third party type but with no luck. In the documentation of Injectable it does say it's possible to register it manually in the configure function, but I was unable to find a working example and failed to implement it successfully by myself.

The project is a command line app in Dart and I am using the SDK and package versions below:

Dart SDK version: 2.15.1 (stable)
get_it: ^7.2.0
injectable: ^1.5.0
injectable_generator: ^1.5.0

Thanks for your help!

You can use a RegisterModule for this purpose.

you can check how you can use RegisterModule for injectable from here

@module  
abstract class RegisterModule {  
   YourThirdParty getThirdPartyObjectInstance(TypeOfThirdParty typedObject, @factoryParam String url) => YourThirdParty(obj, url);  
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM