• Optimization
  • Navigation
  • Tracking
  • Maps
  • Places

Simple Asset Tracking Example

This example shows:

  • Initialize asset tracking: This involves setting up the asset tracking module with necessary configurations such as API keys, data tracking configurations, and fake GPS settings.
  • Add Asset tracking callback: Implement callbacks to handle events such as successful location updates, failures, start of tracking, and stop of tracking.
  • Create and Bind Asset: Create an asset profile with a custom ID, name, description, and attributes. It then creates and binds this asset to the asset tracking system.
  • Check permissions and start tracking: Check for location permissions, and if granted, it starts tracking the asset's location. Provides options to start and stop tracking.

simple_tracking.dart view source

1import 'dart:io';
2import 'package:flutter/material.dart';
3import 'package:nb_asset_tracking_flutter/nb_asset_tracking_flutter.dart';
4import 'package:nb_asset_tracking_flutter_example/util/consts.dart';
5import 'package:nb_asset_tracking_flutter_example/util/permiss_checker.dart';
6import 'package:nb_asset_tracking_flutter_example/util/toast_mixin.dart';
7import 'package:uuid/uuid.dart';
9class SimpleTrackingExample extends StatefulWidget {
11 SimpleTrackingExampleState createState() => SimpleTrackingExampleState();
14class SimpleTrackingExampleState extends State<SimpleTrackingExample>
15 with ToastMixin
16 implements OnTrackingDataCallBack {
17 bool bindAsset = false;
18 final assetTracking = AssetTracking();

This Flutter code snippet demonstrates a simple asset tracking example using a stateful widget and a mixin called ToastMixin to display toast messages. Here's a summary of the functionalities:

  • Initialize asset tracking:

    • In the initState() method, the initAssetTracking() function is called to initialize asset tracking. It sets up the necessary configurations and listeners.
  • Add Asset tracking callback:

    • The SimpleTrackingExampleState class implements the OnTrackingDataCallBack interface, which defines callback methods for handling tracking events such as location updates and tracking status changes.
  • Create and Bind Asset:

    • The createAndBindAssetId() method creates a new asset profile and binds it to the asset tracking service. It generates a unique ID for the asset and updates the UI accordingly.
  • Check permissions and start tracking:

    • The UI consists of two buttons, "Start Tracking" and "Stop Tracking," which are enabled based on certain conditions. The "Start Tracking" button triggers the start of tracking if all conditions are met, including location permission on Android and the presence of a bound asset ID.
  • Handle tracking events:

    • The onLocationSuccess() method updates the UI with location information whenever a new location is received.
    • The onTrackingStart() and onTrackingStop() methods update the UI to reflect the tracking status.
  1. Initialize Asset Tracking

    1. assetTracking.initialize(apiKey: accessKey);
    2. assetTracking.addDataListener(this);
  2. Create and Bind Asset

    1. AssetProfile profile = AssetProfile(...)
    2. AssetResult result = await assetTracking.createAsset(profile: profile);
    3. var assetResult = await assetTracking.bindAsset(customId: assetID);
  3. Check Permissions and Start Tracking

    1. var granted = await checkAndRequestLocationPermission();
    2. assetTracking.startTracking();
    3. assetTracking.stopTracking();
  4. Handle Tracking Events

    1. onLocationSuccess(NBLocation location)
    2. onTrackingStart(String message)
    3. onTrackingStop(String message)

Have Questions ?