flutter_background_geolocation 0.1.5

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 81

flutter_background_geolocation


The most sophisticated background location-tracking & geofencing module with battery-conscious motion-detection intelligence for iOS and Android.

The plugin's Philosophy of Operation is to use motion-detection APIs (using accelerometer, gyroscope and magnetometer) to detect when the device is moving and stationary.

  • When the device is detected to be moving, the plugin will automatically start recording a location according to the configured distanceFilter (meters).

  • When the device is detected be stationary, the plugin will automatically turn off location-services to conserve energy.

Also available for Cordova, React Native, NativeScript and pure native apps.


The Android module requires purchasing a license. However, it will work for DEBUG builds. It will not work with RELEASE builds without purchasing a license.

(2018) This plugin is supported full-time and field-tested daily since 2013.


Google Play

Home Settings

Contents

🔷 Installing the Plugin

📂 pubspec.yaml:

dependencies:
  flutter_background_geolocation: '^0.1.0'

Or latest from Git:

dependencies:
  flutter_background_geolocation:
    git:
      url: https://github.com/transistorsoft/flutter_background_geolocation

🔷 Setup Guides

🔷 Using the plugin

import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;

⚠ī¸ Note as bg in the import. This is important to namespace the plugin's classes, which often use common class-names such as Location, Config, State. You will access every flutter_background_geolocation class with the prefix bg (ie: "background geolocation").

🔷 Example

Full Example

There are three main steps to using BackgroundGeolocation:

  1. Wire up event-listeners.
  2. Configure the plugin with #ready.
  3. #start the plugin.

import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;

class _MyHomePageState extends State<MyHomePage> {

  @override
  void initState() {
    ////
    // 1.  Listen to events (See docs for all 12 available events).
    //

    // Fired whenever a location is recorded
    bg.BackgroundGeolocation.onLocation((bg.Location location) {
      print('[location] - $location');
    });

    // Fired whenever the plugin changes motion-state (stationary->moving and vice-versa)
    bg.BackgroundGeolocation.onMotionChange((bg.Location location) {
      print('[motionchange] - $location');
    });

    // Fired whenever the state of location-services changes.  Always fired at boot
    bg.BackgroundGeolocation.onProviderChange((bg.ProviderChangeEvent event) {
      print('[providerchange] - $event');
    });

    ////
    // 2.  Configure the plugin
    //
    bg.BackgroundGeolocation.ready(bg.Config(
        desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
        distanceFilter: 10.0,
        stopOnTerminate: false,
        startOnBoot: true,
        debug: true,
        logLevel: bg.Config.LOG_LEVEL_VERBOSE,
        reset: true
    )).then((bg.State state) {
      if (!state.enabled) {
        ////
        // 3.  Start the plugin.
        //
        bg.BackgroundGeolocation.start();
      }
    });
  }
}

ℹī¸ NOTE: The configuration {} provided to the #ready method is applied only when your app is first booted — for every launch thereafter, the plugin will automatically load the last known configuration from persistant storage. If you wish to force the #ready method to always apply the supplied config {}, you can specify reset: true

bg.BackgroundGeolocation.ready(bg.Config(
  reset: true,  // <-- true to always apply the supplied config
  distanceFilter: 10.0
)).then((bg.State state) {
  print('[ready] success: $state);
});

⚠ī¸ Do not execute any API method which will require accessing location-services until the callback to **#ready* executes (eg: #getCurrentPosition, #watchPosition, #start).

🔷 Demo Application

A fully-featured Demo App is available in the repo in the /example folder.

Home Settings

🔷 Simple Testing Server

A simple Node-based web-application with SQLite database is available for field-testing and performance analysis. If you're familiar with Node, you can have this server up-and-running in about one minute.

License

The MIT License (MIT)

Copyright (c) 2018 Chris Scott, Transistor Software

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Change Log

Unreleased

[0.1.5] - 2018-09-25

  • [Fixed] warnings reported by Flutter publisher.

[0.1.4] - 2018-09-25

  • [Added] Android Headless mode. See docs for enableHeadless.
  • [Added] an "event list" screen to "AdvancedApp" example.
  • [Added] error callback for BackgroundGeolocation.onLocation.
  • [Fixed] Issue #5 podspec property s.static_framework = true to allow the plugin to work in apps where use_frameworks! directive is applied in Podfile (Thanks to @vova-beloded).

[0.1.3] - 2018-09-20

  • Implementing Dart-style docs with dartdoc
  • Re-organize /lib dir; remove /src folder (hides classes from dartdoc).

[0.1.2] - 2018-09-17

  • Implementing advanced example app.
  • Fix missing removeListener methods for enabledchange, connectivitychange
  • Implement destroyLog
  • Implement instrumented testing in example/test_driver. See examples/scripts/test.sh
  • Fix warnings.

[0.1.1] - 2018-09-15

  • Implementing advanced example app.
  • Fix bug in getGeofences

[0.1.0] - 2018-09-13

  • First working beta release.

[0.0.1] - 2018-07-07

  • Reserve namespace. Plugin not yet implemented.

example/lib/main.dart

import 'package:flutter/material.dart';

import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
import 'package:flutter_background_geolocation_example/app.dart';
import 'advanced/app.dart';
import 'hello_world/app.dart';

/// Receives events from BackgroundGeolocation in Headless state.
///
void headlessTask(bg.HeadlessEvent headlessEvent) async {
  print('đŸ“Ŧ --> $headlessEvent');

  switch(headlessEvent.name) {
    case bg.Event.TERMINATE:
      try {
        bg.Location location = await bg.BackgroundGeolocation.getCurrentPosition(samples: 1);
        print('[getCurrentPosition] Headless: $location');
      } catch (error) {
        print('[getCurrentPosition] Headless ERROR: $error');
      }
      break;
    case bg.Event.HEARTBEAT:
      try {
        bg.Location location = await bg.BackgroundGeolocation.getCurrentPosition(samples: 1);
        print('[getCurrentPosition] Headless: $location');
      } catch (error) {
        print('[getCurrentPosition] Headless ERROR: $error');
      }
      break;
    case bg.Event.LOCATION:
      bg.Location location = headlessEvent.event;
      break;
    case bg.Event.MOTIONCHANGE:
      bg.Location location = headlessEvent.event;
      break;
    case bg.Event.GEOFENCE:
      bg.GeofenceEvent geofenceEvent = headlessEvent.event;
      break;
    case bg.Event.GEOFENCESCHANGE:
      bg.GeofencesChangeEvent event = headlessEvent.event;
      break;
    case bg.Event.SCHEDULE:
      bg.State state = headlessEvent.event;
      break;
    case bg.Event.ACTIVITYCHANGE:
      bg.ActivityChangeEvent event = headlessEvent.event;
      break;
    case bg.Event.HTTP:
      bg.HttpEvent response = headlessEvent.event;
      break;
    case bg.Event.POWERSAVECHANGE:
      bool enabled = headlessEvent.event;
      break;
    case bg.Event.CONNECTIVITYCHANGE:
      bg.ConnectivityChangeEvent event = headlessEvent.event;
      break;
    case bg.Event.ENABLEDCHANGE:
      bool enabled = headlessEvent.event;
      break;
  }
  // Important:  must signal completion of our headless task to native code.
  headlessEvent.finish();
}

void main() {
  /// Application selection:  Select the app to boot:
  /// - AdvancedApp
  /// - HelloWorldAp
  /// - HomeApp
  ///
  SharedPreferences.getInstance().then((SharedPreferences prefs) {
    String appName = prefs.getString("app");
    switch(appName) {
      case AdvancedApp.NAME:
        runApp(new AdvancedApp());
        break;
      case HelloWorldApp.NAME:
        runApp(new HelloWorldApp());
        break;
      default:
        // Default app.  Renders the application selector home page.
        runApp(new HomeApp());
    }
  });

  /// Register Headless Task.
  bg.BackgroundGeolocation.registerHeadlessTask(headlessTask);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_background_geolocation: ^0.1.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_background_geolocation/flutter_background_geolocation.dart';
  
Version Uploaded Documentation Archive
0.1.5 Sep 25, 2018 Go to the documentation of flutter_background_geolocation 0.1.5 Download flutter_background_geolocation 0.1.5 archive
0.1.4 Sep 25, 2018 Go to the documentation of flutter_background_geolocation 0.1.4 Download flutter_background_geolocation 0.1.4 archive
0.1.3 Sep 20, 2018 Go to the documentation of flutter_background_geolocation 0.1.3 Download flutter_background_geolocation 0.1.3 archive
0.1.2 Sep 17, 2018 Go to the documentation of flutter_background_geolocation 0.1.2 Download flutter_background_geolocation 0.1.2 archive
0.1.1 Sep 15, 2018 Go to the documentation of flutter_background_geolocation 0.1.1 Download flutter_background_geolocation 0.1.1 archive
0.1.0 Sep 13, 2018 Go to the documentation of flutter_background_geolocation 0.1.0 Download flutter_background_geolocation 0.1.0 archive
0.0.1 Jul 7, 2018 Go to the documentation of flutter_background_geolocation 0.0.1 Download flutter_background_geolocation 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
62
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
81
Learn more about scoring.

We analyzed this package on Sep 25, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.3
  • Flutter: 0.8.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

Fix lib/background_geolocation.dart.

Analysis of lib/background_geolocation.dart reported 1 hint:

line 141 col 22: The value of the field '_eventsLocationError' isn't used.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
device_info ^0.2.1 0.2.1
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8