Why can't using BarcodeScanner inside a service?

As title, I got a runtime error:

Can’t resolve all parameters for ScannerService: (?).

codes :

   import { BarcodeScanner } from '@ionic-native/barcode-scanner';

    export class ScannerService{
            private barcodeScanner: BarcodeScanner    
            ) {
            console.log('Hello ScannerService Provider');

If I remove private barcodescanner, it runs fine.
Anyone can help?

Did you also import the barcodeScanner in your app.module.ts? This error is telling you that the BarcdeScanner isn’t provided. So double check if you added the BarcodeScanner to your providers :slight_smile: in app.module

1 Like

Yes, already did that, and double checked, like below:

providers: [
{provide: ErrorHandler, useClass: IonicErrorHandler}

Anything else need I check?

And is the BarcodeScanner working from any other parts or pages inside your app (so outside of the service)

BTW you didn’t define your service with @Injectable() prefix. Thats whats possibly is going wrong?

Oh YES, thank you~

I put this Injectable line in other 2 service but forgot this one …

haha okay glad it’s working now. Just use the CLI generator with ionic g provider myProvider/myService to create it automatically with an @Injectable() decorator.

OK, thank you~~
This cli is really handy~