File plugin - No provider for File!

Hi, i just installed File in my app, version “@ionic-native/file”: “^3.2.1”.

But when I get the error "No provider for File!!

I just followed the documentation.

This is the code.


import { File } from ‘@ionic-native/file’;

constructor(public navCtrl: NavController,
public navParams: NavParams,
public storage: Storage,
public file: File
) { }

lalala() {
this.file.checkDir(this.file.dataDirectory, ‘mydir’).then(_ => console.log(‘Directory exists’)).catch(err => console.log(‘Directory doesnt exist’));
}

Any clue?? Thank you.

Any help about this?

I dont know what can I do, doesnt matter what i try, always says No provider for File!

i use this instead
var file = new File();
and everything works fine.

You have to declare your provider in app.modules.ts

Exactly where did you used that line of code?

You need to upgrade your project to the latest Ionic Native 3.x++

Here’s an example of including Speech Recognition:

Ensure you’ve included File in your providers within app.module.

Hi!, as I said in the post earlier, I’m having problems with every single plugin.

Now its the same with File plugin, and Social Sharing plugin.

I’m following the docs from Ionic 2, but both give me the same error, in both of them. No provider for…

Here is an image with my package.json, i think i have the ionic-native up to date.

Thanks!!

on the constructor of my page

Nop, that doesnt work :frowning:

i share part of my code

	saveInFile(passed) {
	var file = new File();
	var date = new Date();
	var text = `${this.rpc.username}  ${this.tag.connected.id}   ${date.toLocaleDateString()}  ${passed? 'OK' : 'INCOMPLETE'} \n`;
	var dir = file.externalRootDirectory + "bt-prod"
	console.log(dir);
	file.checkDir(file.externalRootDirectory, "bt-prod").then((res)=>{
		if(!res){
			file.createDir(file.externalRootDirectory, "bt-prod", false).then(()=>{
				console.log("Directory writed");
			}).catch((err)=>{
				console.error("error creating dir",err);
			});
		}
	});
	
	setTimeout(()=>{
		file.checkFile(dir, "bt-prod-" +  date.getDate() +"-"+date.getMonth() + ".txt").then((res)=>{
			if(!res){
				file.createFile(dir, "bt-prod-"+ date  +".txt",false).then(()=>{
					console.log("file created");
				}).catch((err)=>{
					console.error("error creating file");
				});
			}
		});
	},500);

	setTimeout(()=>{
		file.writeExistingFile(dir, "bt-prod" +  date.getDate() +"-"+date.getMonth() + ".txt", text).then(()=>{
			console.log("file writed");
		}).catch((err)=>{
			console.error("error  writing file",err);
		});
	},1000)
}

Did you add File to providers in app.module?

Since the introduction of Ionic-Native 3.2.1, the plugins have to be declared as providers, see documentation:

1 Like

Yes, I already did it, its declared in the providers list.

Ok, sorry to hear that. I checked what I’ve got, I use version 3.2.2 but I don’t think that’s the difference.

Otherwise, just in case, I really just do that:

app.module.ts:

import {File} from '@ionic-native/file';
    ....
providers: [
   ...
   File,
   ....

using it:

import {File, DirectoryEntry} from '@ionic-native/file';

....

constructor(private file: File) {
 }

 ...

  let fs: string = cordova.file.cacheDirectory;
 ...
   this.file.resolveDirectoryUrl(fs).then((rootDir: DirectoryEntry) => {
       console.log('Coolio');
   }, (errRoot: any) => {
       console.log('Less coolio');
  });

works for me (of course not in the browser).

3 Likes

I’ve been stalking this thread since yesterday, finally got something useful out of it. Thanks!

1 Like

It’s what i said to do

1 Like

I already had everything you said, but I deleted the lines, and wrote it again, now its working, dont know what was different exactly, but now its working.

Thank you all!

1 Like

that’s is the correct way

import { File } from ‘@ionic-native/file’;

constructor(public navCtrl: NavController,
public navParams: NavParams,
public storage: Storage,
public file: File
) { }

lalala() {
this.file.checkDir(this.file.dataDirectory, ‘mydir’).then(_ => console.log(‘Directory exists’)).catch(err => console.log(‘Directory doesnt exist’));
}

Solo tienen que leer la documentación y se ahorrarán dolores de cabeza.

Put the provider in the @component declaration:


@Component({
  selector: 'xxx',
  templateUrl: 'xxx',
  providers: [File]
})
1 Like

this Works for me :+1: