Adding and Using plugins in Ionic 2


#1

I have a very simple app right now with a few pages and not much functionality. I’m trying to get the hang of plugins, and figured that a simple flashlight was a good place to start.

So I did cordova plugin add cordova-plugin-flashlight

And in my .js file I have a simple function that will do
{ console.log("Reached"); Flashlight.toggle(); }

with the import
import {Flashlight} from 'ionic-native';

I know it’s getting called correctly when a specific button is pressed because the log file updates before crashing. The error is

ORIGINAL EXCEPTION: TypeError: Cannot read property 'flashlight' of undefined

So what am I missing? It seems that window.plugins is not recognized yet.

EDIT: I fixed the problem I was having by using Flashlight.toggle() instead. Now it doesn’t throw errors, but still doesn’t turn the light on! I’m going to try it on an older Android.

EDIT 2: Fixed! The plugin was outdated for Android 6.* so I ran it on an older phone and it worked just fine.


#2

Try to follow the official docs, for Flashlight check this:

You may try to run:

ionic plugin add cordova-plugin-flashlight

and try again, avoiding window.plugins.flashlight :slight_smile: