Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing provider for Ionic's Network native plugin

I've tried using Ionics native plugin "Network" but it fails because of a supposedly missing provider. In order to avoid any mistakes I've installed a fresh installation of Ionic and the required dependencies:

ionic cordova plugin add cordova-plugin-network-information
npm install @ionic-native/network

Then I've added the dependency to home's constructor:

import { Component } from '@angular/core';
import { Network } from '@ionic-native/network/ngx';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  constructor(private network: Network) {
      // to nothing ...
  }
}

... and will get the following error message after executing ionic serve:

ERROR Error: Uncaught (in promise): 
Error: StaticInjectorError(AppModule)[HomePage -> Network]: 
  StaticInjectorError(Platform: core)[HomePage -> Network]: 
    NullInjectorError: No provider for Network!
Error: StaticInjectorError(AppModule)[HomePage -> Network]: 
  StaticInjectorError(Platform: core)[HomePage -> Network]: 
    NullInjectorError: No provider for Network!

System information

Ionic:

   ionic (Ionic CLI)             : 4.12.0 (/Users/samnowakowski/node_modules/ionic)
   Ionic Framework               : @ionic/angular 4.4.0
   @angular-devkit/build-angular : 0.13.9
   @angular-devkit/schematics    : 7.3.9
   @angular/cli                  : 7.3.9
   @ionic/angular-toolkit        : 1.5.1

Cordova:

   cordova (Cordova CLI) : 9.0.0 ([email protected])
   Cordova Platforms     : none
   Cordova Plugins       : no whitelisted plugins (1 plugins total)

System:

   Android SDK Tools : 26.1.1 (/Users/admin/Library/Android/sdk)
   NodeJS            : v10.15.3 (/usr/local/bin/node)
   npm               : 6.9.0
   OS                : macOS Mojave
   Xcode             : Xcode 10.2.1 Build version 10E1001
like image 415
Lairg Avatar asked May 09 '19 10:05

Lairg


1 Answers

Open app module (app.module.ts) and below code :

import { Network } from '@ionic-native/network/ngx';

@NgModule(
   ...
   providers: [
      Network
   ]
); 
like image 103
Khurshid Ansari Avatar answered Nov 18 '22 07:11

Khurshid Ansari