location_background_plugin 0.1.0+3

  • Readme
  • Changelog
  • Example
  • Installing
  • 68

Flutter Background Execution Sample - LocationBackgroundPlugin #

An example Flutter plugin that showcases background execution using iOS location services.

This plugin is not being actively maintained and is not for production use. An archive of previous versions can be found in the Flutter plugins repository.

Getting Started #

NOTE: This plugin does not currently have an Android implementation.

To import, add the following to your Dart file:

import 'package:location_background/location_background.dart';

Example usage:

import 'package:location_background/location_background.dart';

final locationManager = LocationBackgroundPlugin();

void locationUpdateCallback(Location location) {
  print('Location Update: $location');

Future<void> startMonitoringLocationChanges() =>
Future<void> stopMonitoringLocationChanges() =>

WARNING: do not maintain volatile state or perform long running operations in the location update callback. There is no guarantee from the system for how long a process can perform background processing after a location update, and the Dart isolate may shutdown during execution at the request of the system.

For help getting started with Flutter, view our online documentation.

For help on editing plugin code, view the documentation.

0.1.0+3 #

  • Update README.md indicating package support status.

0.1.0+2 #

  • Fix Dart deprecation warnings.

0.1.0+1 #

  • Log a more detailed warning at build time about the previous AndroidX migration.

0.1.0 #

  • Breaking change. Migrate from the deprecated original Android Support Library to AndroidX. This shouldn't result in any functional changes, but it requires any Android apps using this plugin to also migrate if they're using the original support library.

0.0.2 #

  • Added missing flutter_test package dependency.
  • Added missing flutter version requirements.

0.0.1 #

  • Initial release.


// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file

import 'dart:isolate';
import 'dart:ui' hide TextStyle;

import 'package:flutter/material.dart';
import 'package:location_background_plugin/location_background_plugin.dart';

import 'background.dart';

void main() {

class MyApp extends StatefulWidget {
  _MyAppState createState() {
    return _MyAppState();

class _MyAppState extends State<MyApp> {
  ReceivePort _foregroundPort = ReceivePort();
  LocationBackgroundPlugin _locationPlugin;
  Location _lastLocation;
  bool _isTracking = false;

  void initState() {
    _lastLocation = Location(-1.0, 0.0, 0.0, -1.0, -1.0);

  void initPlatformState() {
    // The IsolateNameServer allows for us to create a mapping between a String
    // and a SendPort that is managed by the Flutter engine. A SendPort can
    // then be looked up elsewhere, like a background callback, to establish
    // communication channels between isolates that were not spawned by one
    // another.
    if (!IsolateNameServer.registerPortWithName(
        _foregroundPort.sendPort, kLocationPluginPortName)) {
      throw 'Unable to register port!';

    // Listen on the port for location updates from our background callback.
    _foregroundPort.listen((dynamic message) {
      final Location location = Location.fromJson(message);
      print('UI Location: $location');
      setState(() {
        _lastLocation = location;
    }, onDone: () {
      // Remove the port mapping just in case the UI is shutting down but
      // background isolate is continuing to run.
    _locationPlugin ??= LocationBackgroundPlugin();

  String _padZero2(int i) => i.toString().padLeft(2, '0');

  String _formatTime(DateTime t) {
    t = t.toLocal();
    final int hour = t.hour;
    final String minute = _padZero2(t.minute);
    final String second = _padZero2(t.second);
    final int year = t.year;
    return '$hour:$minute:$second $year';

  Widget build(BuildContext context) {
    const TextStyle boldText = TextStyle(fontWeight: FontWeight.bold);
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: const Text('Background Plugin Demo'),
            body: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  const Center(
                      child: Text(
                    'Update Time:',
                    style: boldText,
                  Center(child: Text('${_formatTime(_lastLocation.time)}')),
                  const Center(
                      child: Text(
                    style: boldText,
                      child: Text(
                          '(${_lastLocation.latitude}, ${_lastLocation.longitude})')),
                  const Center(
                      child: Text(
                    style: boldText,
                  Center(child: Text('${_lastLocation.altitude} m')),
                  const Center(
                      child: Text(
                    'Speed (meters per second)',
                    style: boldText,
                  Center(child: Text('${_lastLocation.speed} m/s')),
                      child: RaisedButton(
                        Text(_isTracking ? 'Stop Tracking' : 'Start Tracking'),
                    onPressed: () async {
                      if (!_isTracking) {
                        await _locationPlugin.monitorSignificantLocationChanges(
                      } else {
                        await _locationPlugin.cancelLocationUpdates();
                      setState(() {
                        _isTracking = !_isTracking;

Use this package as a library

1. Depend on it

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

  location_background_plugin: ^0.1.0+3

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:

import 'package:location_background_plugin/location_background_plugin.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Mar 31, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Maintenance issues and suggestions

Homepage URL doesn't exist. (-20 points)

At the time of the analysis the homepage field https://github.com/flutter/plugins/tree/master/packages/location_background was unreachable.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


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