flutter_geofire 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 87

flutter_geofire #

A Flutter plugin to use the GeoFire Api

For Flutter plugins for other products, see mrdishant@github

Note: This plugin is still under development, and some APIs might not be available yet. Feedback and Pull Requests are most welcome!

Usage #

GeoFire — Realtime location queries with Firebase.

GeoFire is an open-source library that allows you to store and query a set of keys based on their geographic location.

At its heart, GeoFire simply stores locations with string keys. Its main benefit however, is the possibility of querying keys within a given geographic area - all in realtime.

GeoFire uses the Firebase database for data storage, allowing query results to be updated in realtime as they change. GeoFire selectively loads only the data near certain locations, keeping your applications light and responsive, even with extremely large datasets.

Quickstart #

Initalize GeoFire with path to keys in Realtime Database

String pathToReference = "Sites";
Intializing geoFire
Geofire.initialize(pathToReference);

Also don't forget to add indexOn in your Realtime Database rules

Example: Just change the 'Sites' with your pathToReference

    {
      "rules": {
        ".read":true,
        ".write": true,
          "Sites": {
          ".indexOn": ["g"]
        }
      }
    }   




Setting location data #

Here setLocation method is used and first is the unique id of the place and other two parameters are latitude and longitude of that place.

bool response = await Geofire.setLocation(
        new DateTime.now().millisecondsSinceEpoch.toString(),
        30.730743,
        76.774948)
        

Retrieving a location #

Retrieving a location for a single key in GeoFire happens like below:

Map<String, dynamic> response =
        await Geofire.getLocation("AsH28LWk8MXfwRLfVxgx");

print(response);
        

Geo Queries #

GeoFire allows you to query all keys within a geographic area using GeoQuery objects. As the locations for keys change, the query is updated in realtime and fires events letting you know if any relevant keys have moved. GeoQuery parameters can be updated later to change the size and center of the queried area.

Geofire.queryAtLocation(30.730743, 76.774948, 5).listen((map) {
        print(map);
        if (map != null) {
          var callBack = map['callBack'];

          //latitude will be retrieved from map['latitude']
          //longitude will be retrieved from map['longitude']

          switch (callBack) {
            case Geofire.onKeyEntered:
              keysRetrieved.add(map["key"]);
              break;

            case Geofire.onKeyExited:
              keysRetrieved.remove(map["key"]);
              break;

            case Geofire.onKeyMoved:
            // Update your key's location
              break;

            case Geofire.onGeoQueryReady:
            // All Intial Data is loaded
            print(map['result'])

              break;
          }
        }

        setState(() {});

Stop Listening to Geo Query #

To remove listeners to all queries:

bool response = await Geofire.stopListener();

print(response);

Removing a location #

To remove a location and delete it from the database simply pass the location's key to removeLocation:

bool response = await Geofire.removeLocation("AsH28LWk8MXfwRLfVxgx");

print(response);                

Contributing #

if you want to contribute to GeoFire, clone the repository and just start making pull requests.

git clone 'https://github.com/mrdishant/flutter_geofire'
This plugin is in development suggestions are welcome. Happy Coding!!! #

1.0.0 #

  • Remove Query Listeners Feature added

0.0.5 #

  • Stream added for query with all callbacks.

0.0.4 #

  • Implementation

0.0.3 #

  • Implementation

0.0.2 #

  • Implementation

0.0.1 #

  • TODO: Describe initial release.

example/README.md

fluter_geofire_example #

Demonstrates how to use the fluter_geofire plugin.

Getting Started #

For Flutter plugins for other products, see mrdishant@github

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_geofire/flutter_geofire.dart';

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {
  List<String> keysRetrieved = [];

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

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String pathToReference = "Sites";

    //Intializing geoFire
    Geofire.initialize(pathToReference);

    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      Geofire.queryAtLocation(30.730743, 76.774948, 5).listen((map) {
        print(map);
        if (map != null) {
          var callBack = map['callBack'];

          //latitude will be retrieved from map['latitude']
          //longitude will be retrieved from map['longitude']

          switch (callBack) {
            case Geofire.onKeyEntered:
              keysRetrieved.add(map["key"]);
              break;

            case Geofire.onKeyExited:
              keysRetrieved.remove(map["key"]);
              break;

            case Geofire.onKeyMoved:
//              keysRetrieved.add(map[callBack]);
              break;

            case Geofire.onGeoQueryReady:
//              map["result"].forEach((key){
//                keysRetrieved.add(key);
//              });

              break;
          }
        }

        setState(() {});
      }).onError((error) {
        print(error);
      });
    } on PlatformException {
//      response = '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;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.all(20.0),
              ),
              Center(
                child: keysRetrieved.length > 0
                    ? Text("First key is " +
                        keysRetrieved.first.toString() +
                        "\nTotal Keys " +
                        keysRetrieved.length.toString())
                    : CircularProgressIndicator(),
              ),
              Padding(
                padding: EdgeInsets.all(10.0),
              ),
              Center(
                child: RaisedButton(
                  onPressed: () {
                    setLocation();
                  },
                  color: Colors.blueAccent,
                  child: Text(
                    "Set Location",
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.all(10.0),
              ),
              Center(
                child: RaisedButton(
                  onPressed: () {
                    setLocationFirst();
                  },
                  color: Colors.blueAccent,
                  child: Text(
                    "Set Location AsH28LWk8MXfwRLfVxgx",
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.all(10.0),
              ),
              Center(
                child: RaisedButton(
                  onPressed: () {
                    getLocation();
                  },
                  color: Colors.blueAccent,
                  child: Text(
                    "Get Location AsH28LWk8MXfwRLfVxgx",
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.all(10.0),
              ),
              Center(
                child: RaisedButton(
                  onPressed: () {
                    removeLocation();
                  },
                  color: Colors.blueAccent,
                  child: Text(
                    "Remove Location AsH28LWk8MXfwRLfVxgx",
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.all(10.0),
              ),
              Center(
                child: RaisedButton(
                  onPressed: () {
                    removeQueryListener();
                  },
                  color: Colors.blueAccent,
                  child: Text(
                    "Remove Query Listener",
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ],
          )),
    );
  }

  void setLocation() async {
    bool response = await Geofire.setLocation(
        new DateTime.now().millisecondsSinceEpoch.toString(),
        30.730743,
        76.774948);

    print(response);
  }

  void setLocationFirst() async {
    bool response =
        await Geofire.setLocation("AsH28LWk8MXfwRLfVxgx", 30.730743, 76.774948);

    print(response);
  }

  void removeLocation() async {
    bool response = await Geofire.removeLocation("AsH28LWk8MXfwRLfVxgx");

    print(response);
  }

  void removeQueryListener() async {
    bool response = await Geofire.stopListener();

    print(response);
  }

  void getLocation() async {
    Map<String, dynamic> response =
        await Geofire.getLocation("AsH28LWk8MXfwRLfVxgx");

    print(response);
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_geofire: ^1.0.2

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:flutter_geofire/flutter_geofire.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
76
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
95
Overall:
Weighted score of the above. [more]
87
Learn more about scoring.

We analyzed this package on Oct 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-1 points)

13 out of 13 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Maintenance suggestions

The description is too long. (-5 points)

Search engines display only the first part of the description. Try to keep the value of the description field in your package's pubspec.yaml file between 60 and 180 characters.

Dependencies

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