bg_location 0.0.1 copy "bg_location: ^0.0.1" to clipboard
bg_location: ^0.0.1 copied to clipboard

PlatformiOS

Background location tracking using Significant-Change Location Service only (iOS Only)

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:bg_location/bg_location.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

  @override
  void initState() {
    super.initState();
    _initPlatform();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> _initPlatform() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await BgLocation.platformVersion ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  Future _startTracking() async {
    BgLocation.getLocationUpdates((location) => print('[my sample app] Location update received: $location'));
    await BgLocation.startTracking();
  }

  Future _stopTracking() async {
    await BgLocation.stopTracking();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
        floatingActionButton: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            FloatingActionButton.extended(
              onPressed: _initPlatform,
              label: Text("Platform"),
              icon: Icon(Icons.settings),
            ),
            FloatingActionButton.extended(
              onPressed: _startTracking,
              label: Text("Start"),
              icon: Icon(Icons.play_arrow),
            ),
            FloatingActionButton.extended(
              onPressed: _stopTracking,
              label: Text("Stop"),
              icon: Icon(Icons.stop),
            )
          ],
        ),
      ),
    );
  }
}
1
likes
110
pub points
11%
popularity

Publisher

unverified uploader

Background location tracking using Significant-Change Location Service only (iOS Only)

Homepage

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on bg_location