Pannam
Pannam

Reputation: 721

Instance method can't be accessed from static method

I have a boolean bool isDriverAvailable=false which will check if the user is online or not, if it is online then the GeoFire plugin will update its latitude and longitude to the database. I am using a background location for this project which has a callback static void backgroundLocationCallBack(LocationDto location) method where I plan to implement this check and update the location. this is my code so far

class _HomeTabPageState extends State<HomeTabPage>
    with AutomaticKeepAliveClientMixin, OSMMixinObserver {
  bool isDriverAvailable = false;

//some code

static void backgroundLocationCallBack(LocationDto location) async {
    print("from background location +${location.latitude}");
        if (isDriverAvailable)
        Geofire.setLocation(
            currentFirebaseUser!.uid, location.latitude!, location.longitude!);

  }

  void getLocationLiveUpdates() async {
    return await BackgroundLocator.registerLocationUpdate(
        backgroundLocationCallBack,
        autoStop: false,
        // disposeCallback: backgroundLocationDisposeCallBack,

        androidSettings: AndroidSettings(
            accuracy: LocationAccuracy.NAVIGATION,
            interval: 5,
            distanceFilter: 0,
            client: LocationClient.google,
            androidNotificationSettings: AndroidNotificationSettings(
              notificationChannelName: 'Location tracking',
              notificationTitle: 'Start Location Tracking',
              notificationMsg: 'Track location in background',
              notificationBigMsg:
                  'Background location is on to keep the app up-tp-date with your location. This is required for main features to work properly when the app is not running.',
              notificationIconColor: Colors.grey,
            )));
  }

I get an error on if (isDriverAvailable) The error

Instance members can't be accessed from a static method.
Try removing the reference to the instance member, or removing the keyword 'static' from the method.

I can't remove the static keyboard on backgroundLocationCallBack(LocationDto location) as it is only meant to work with static method. How else can I implement the bool check ?

Upvotes: 1

Views: 2036

Answers (1)

Me&#239; M.
Me&#239; M.

Reputation: 321

You will need to somehow pass the isDriverAvailable boolean to your function, remove the static from the method, or refactor the whole thing.

Static members of a class need to be available from a class-level (in comparison to usual members being available on instance-level), which means that you don't get the implicit this member, as it is assumed you will call the function from the outside the instance.

I gave a quick glance to the background_locator package and its documentation, but nothing states this callback needs to be static, as long as it takes a LocationDto argument and returns void.

Upvotes: 2

Related Questions