Platform.ready in Provider?

How do you access Platform properties in a provider?

import {Platform} from 'ionic-angular';

  constructor(platform: Platform) {
    this.platform = platform;
   this.platform.ready().then(() => {
        console.log("PLATFORM READY IN PROVIDER")
    })
}

Results in TypeError: Cannot read property ‘ready’ of undefined

Ultimately trying to get which Platform it is to be used in the Provider/Service.

I guess platform variable should be defined as property in Class.Declare properly like below.

platform: any;

Thanks for the try but it did not work. Same results.

Maybe you couldn’t or shouldn’t?

In a provider you could not access to components like navcontroller, loadingcontroller etc. maybe it’s the same for the platform. This “force” you to have a nice separation between presentation and logic.

Try like this

constructor(public platform: Platform) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
console.log(“Platform is ready”);
});
}

@ Sathyesh: Still no luck. Same results. Thanks for the try though.
@reedrichards: May be.

Can you please post your complete provider code? Actually its working for me in provider.

Hi Sathyesh.
I made a duplicate of my service and removed portions of it until it worked.
The following was the problem:

static get parameters() {
return [[Http]];
  }

With that in the Provider, the Platform.ready was not working.
However, without that in there, I now get an error: ORIGINAL EXCEPTION: No provider for NavController!

Here is the provider:
import {Page, NavController, Platform} from ‘ionic-angular’;
import {Injectable, Component} from ‘@angular/core’;
import {Http} from ‘@angular/http’;

@Injectable()
export class Test {
 // static get parameters() {
        //return [[Http]];
    //}
  constructor(public nav: NavController, public platform: Platform) {
    platform.ready().then(() => {
      console.log("Platform is ready");
    });
  }

}

Unless you have any thoughts, I might need to create a new post on how to use the Platform.ready AND NavController / static get parameters

Hi ,

I have prepared sample service like yours,its working fine for me(see below).

One more question is that,why would you need navcontroller in service file?What’s the purpose?

import 'rxjs/add/operator/map';
import { NavController, Platform} from 'ionic-angular';
import { Injectable } from '@angular/core';
import {Http} from '@angular/http';


@Injectable()
export class MovieService {

	constructor(private http:Http,public platform:Platform,public nav: NavController) {

      platform.ready().then(() => {
         console.log("Platform is ready");
       });

	}

    searchMovies(movieName) {
        var url = 'http://api.themoviedb.org/3/search/movie?query=&query=' + encodeURI(movieName) + '&api_key=5fbddf6b517048e25bc3ac1bbeafb919';
        var response = this.http.get(url).map(res => res.json());
		return response;
    }
}

Ok, I tried it exactly except for name change of provider. Same errors:
EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in ./MyApp class MyApp_Host - inline template:0:0
ORIGINAL EXCEPTION: No provider for NavController!
ORIGINAL STACKTRACE:
Error: DI Exception
at NoProviderError.BaseException [as constructor] (http://localhost:8100/build/js/app.bundle.js:2868:23)
at NoProviderError.AbstractProviderError [as constructor] (http://localhost:8100/build/js/app.bundle.js:29524:16)
at new NoProviderError (http://localhost:8100/build/js/app.bundle.js:29561:16)
at ReflectiveInjector_.throwOrNull (http://localhost:8100/build/js/app.bundle.js:30558:19)
at ReflectiveInjector
.getByKeyDefault (http://localhost:8100/build/js/app.bundle.js:30586:25)
at ReflectiveInjector
.getByKey (http://localhost:8100/build/js/app.bundle.js:30549:25)
at ReflectiveInjector
.get (http://localhost:8100/build/js/app.bundle.js:30358:21)
at ElementInjector.get (http://localhost:8100/build/js/app.bundle.js:32129:48)
at ElementInjector.get (http://localhost:8100/build/js/app.bundle.js:32129:48)
at DebugAppView.get (MyApp.template.js:25:163)
ERROR CONTEXT:
[object Object]

System info:
Cordova CLI: 6.1.1
Gulp version: CLI version 3.9.0
Gulp local: Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.11
Ionic CLI Version: 2.0.0-beta.37
Ionic App Lib Version: 2.0.0-beta.20
OS:
Node Version: v6.3.1

Please update the environment settings(type ionic info in terminal) of your App.

Cordova CLI: 6.1.1
Gulp version: CLI version 3.9.0
Gulp local: Local version 3.9.1
Ionic Framework Version: 2.0.0-beta.11
Ionic CLI Version: 2.0.0-beta.37
Ionic App Lib Version: 2.0.0-beta.20
OS:
Node Version: v6.3.1

May I see yours as well.

Cordova CLI: 5.4.1
Ionic Framework Version: 2.0.0-beta.11
Ionic CLI Version: 2.0.0-beta.37
Ionic App Lib Version: 2.0.0-beta.20
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Mac OS X Yosemite
Node Version: v6.5.0
Xcode version: Not installed

Thanks for that. There a few options I will try including getActiveNav()
I will also consider not using it at all
That said, you did not get the error when you tried?
I am gone now for most of the day - thanks again!