Quick Summary: This tutorial covers how to implement geofencing in a Flutter app using Bluedot, a geolocation services platform. By following this guide, developers can integrate Bluedot’s API to create location-based triggers, enhancing user engagement through precise geofence detection and action responses.
Want to build your Flutter App?
Discover the power of seamless performance, responsive UI, and cross-platform compatibility with Flutter Apps.
Geofencing is a powerful tool in mobile app development, enabling apps to trigger specific actions when a user enters or exits a defined area. In this tutorial, we will explore how to implement geofencing in a Flutter application using Bluedot, a leading geolocation services platform.
Bluedot's API provides accurate geofencing capabilities, helping developers create location-based experiences with minimal setup. Whether you're building apps for retail, logistics, or travel, this step-by-step guide will walk you through integrating Bluedot into your Flutter project to detect geofences and enhance user engagement.
Let’s dive into the technical details!
Flutter Bluedot Point SDK is a wrapper around the Android Point SDK and iOS Point SDK, allowing for Bluedot integration in Flutter apps. Functionality is provided through one single flutter plugin so you can use it for both platforms without downloading any extra packages
Steps - Bluedot Flutter Tutorial for Geofence
1. Install the Flutter Bluedot Point SDK plugin
$ flutter pub add bluedot_point_sdk
add a line like this to your package’s pubspec.yaml
dependencies:
bluedot_point_sdk: ^1.0.0
Import it
Now in your Dart code, you can use:
import 'package:bluedot_point_sdk/bluedot_point_sdk.dart';
2. . Initialize the SDK
import 'package:flutter/material.dart';
import 'package:bluedot_point_sdk/bluedot_point_sdk.dart';
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
_initializeBluedotPointSDK();
}
void _initializeBluedotPointSDK() {
BluedotPointSdk.instance.initialize('Your_bluedot_project_Id')
.then((value) {
// Successfully initialised Bluedot Point SDK
debugPrint('Bluedot Point SDK has been initialized');
}).catchError((error) {
// Failed to initialize Bluedot Point SDK. Handle error here.
debugPrint('Failed to initialize Bluedot Point SDK. Error: $error');
});
}
@override
Widget build(BuildContext context) {
return Container();
}
}
3. Initialize Status
You can use the isInitialized method to check the status of the SDK. It will return a boolean value.
BluedotPointSdk.instance.isInitialized().then((value) {
debugPrint('Is Bluedot Point SDK initialized?: $value');
});
Start Geo-triggering
Geo-triggering allows the automatic detection of location context change events (such as entering or exiting a geofence, or crossing a Geoline™). For this capability, the SDK needs to be initialized and the app must have location permission.
To start geo-triggering, you should use the GeoTriggeringBuilder .
Android
On android, you can run geo triggering in foreground or background mode.
Foreground mode:
Starting geo-triggering on foreground mode would require notification permissions. From Android 13, you have to request the user for notification permissions.
To run geo triggering on foreground mode, you have to set notification details as below. If any of the parameters below are null or blank (except for androidNotificationId), geo-triggering will start on background mode.
String channelId = 'Your channel Id';
String channelName = 'Your channel Name';
String androidNotificationTitle = 'Your notification title';
String androidNotificationContent = 'Your notification content';
int androidNotificationId = 123; // Will be -1 by default if set to null.
BluedotPointSdk.instance.geoTriggeringBuilder()
.androidNotification(channelId, channelName, androidNotificationChannel, androidNotificationContent, androidNotificationId)
.start().then((value) {
//Handle geo triggering started successfully
debugPrint('Geo-triggering has been started');
}).catchError((error) {
//Handle error when start geo-triggering
debugPrint('Failed to start geo-triggering. Error $error');
});
Background mode:
BluedotPointSdk.instance.geoTriggeringBuilder().start().then((value) {
//Handle geo triggering started successfully
debugPrint('Geo-triggering has been started');
}).catchError((error) {
//Handle error when start geo-triggering
debugPrint('Failed to start geo-triggering. Error $error');
});
iOS
Either of the methods above would start geo-triggering in iOS.
Receiving Geo-trigger events
Note: As Entry events may occur immediately upon staring Geo-triggering, it is recommended ed to subscribe to the events before starting the Geo-triggering service.
final geoTriggeringEventChannel = MethodChannel(BluedotPointSdk.geoTriggering);
geoTriggeringEventChannel.setMethodCallHandler((MethodCall call) async {
var args = call.arguments;
switch (call.method) {
case GeoTriggeringEvents.onZoneInfoUpdate:
debugPrint('On Zone Info Update: $args');
break;
case GeoTriggeringEvents.didEnterZone:
debugPrint('Did Enter Zone: $args');
break;
case GeoTriggeringEvents.didExitZone:
debugPrint('Did Exit Zone: $args');
break;
default:
break;
}
});
Stop Geo-triggering
BluedotPointSdk.instance.stopGeoTriggering().then((value) {
// Successfully stop geo triggering
debugPrint('Geo-triggering has been stopped');
}).catchError((error) {
// Failed to stop geo triggering, handle error in here
debugPrint('Failed to stop geo triggering. Error $error');
});
Geo-triggering Status
BluedotPointSdk.instance.isGeoTriggeringRunning().then((value) {
setState(() {
debugPrint('Is Geo Running: $value');
});
});
Full Code :
import 'package:bluedot_point_sdk/bluedot_point_sdk.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// The SDK needs be initialized and the app must have location permissions.
var _isGeoTriggeringRunning = false;
@override
void initState() {
super.initState();
// Listen to geo triggering event from geo triggering event channel
final geoTriggeringEventChannel = MethodChannel(BluedotPointSdk.geoTriggering);
geoTriggeringEventChannel.setMethodCallHandler((MethodCall call) async {
var args = call.arguments;
switch (call.method) {
case GeoTriggeringEvents.onZoneInfoUpdate:
debugPrint('On Zone Info Update: $args');
break;
case GeoTriggeringEvents.didEnterZone:
debugPrint('Did Enter Zone: $args');
break;
case GeoTriggeringEvents.didExitZone:
debugPrint('Did Exit Zone: $args');
break;
default:
break;
}
});
}
void _startGeoTriggering() {
String channelId = 'Your channel Id';
String channelName = 'Your channel Name';
String androidNotificationTitle = 'Your notification title';
String androidNotificationContent = 'Your notification content';
int androidNotificationId = 123; // Will be -1 by default if set to null.
BluedotPointSdk.instance.geoTriggeringBuilder()
.androidNotification(channelId, channelName, androidNotificationChannel, androidNotificationContent, androidNotificationId)
.start().then((value) {
//Handle geo triggering started successfully
debugPrint('Geo-triggering has been started');
}).catchError((error) {
//Handle error when start geo-triggering
debugPrint('Failed to start geo-triggering. Error $error');
});
}
void _stopGeoTriggering() {
BluedotPointSdk.instance.geoTriggeringBuilder().start().then((value) {
//Handle geo triggering started successfully
debugPrint('Geo-triggering has been started');
}).catchError((error) {
//Handle error when start geo-triggering
debugPrint('Failed to start geo-triggering. Error $error');
});
}
void _updateGeoTriggeringStatus() {
BluedotPointSdk.instance.isGeoTriggeringRunning().then((value) {
setState(() {
_isGeoTriggeringRunning = value;
});
});
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Is Geo Triggering Running: $_isGeoTriggeringRunning'),
ElevatedButton(onPressed: _startGeoTriggering, child: const Text('Start Geo-triggering')),
ElevatedButton(onPressed: _stopGeoTriggering, child: const Text('Stop Geo-triggering')),
ElevatedButton(onPressed: _updateGeoTriggeringStatus, child: const Text('Update Geo-triggering status')),
],
);
}
}
Conclusion
In conclusion, integrating Bluedot’s geofencing capabilities into your Flutter app can significantly enhance user interactions through location-based services. By following this tutorial, you’ve learned how to set up and implement geofences, allowing your app to trigger specific actions when users enter or exit defined areas.
Whether for personalized marketing, navigation, or security, geofencing offers diverse possibilities to elevate user experiences. With Bluedot’s accurate API and Flutter’s flexibility, you can build dynamic, location-aware apps that respond in real-time, improving functionality and engagement.
Get free consultation from the best flutter development company in india to elevate your Flutter app design. Unlock the full potential of Flutter layouts with our professional Flutter developers.