Asset Profile Operations

This example shows:

  • Create an Asset: Create a new asset with a custom ID, name, description, and attributes. The created asset ID is stored in the assetTracking object.
  • Bind an Asset ID: Bind an existing asset ID to the current session. This action associates the current session with a specific asset.
  • Update Current Asset Profile: Update the profile of the current asset. Change the name, description, and attributes of the asset.
  • Retrieve Asset Detail of Current Asset ID: Retrieve detailed information about the current asset, such as its ID, name, description, and attributes.

asset_profile_screen.dart view source

1
import 'dart:convert';
2
3
import 'package:flutter/material.dart';
4
import 'package:nb_asset_tracking_flutter/nb_asset_tracking_flutter.dart';
5
import 'package:nb_asset_tracking_flutter_example/util/consts.dart';
6
import 'package:nb_asset_tracking_flutter_example/util/toast_mixin.dart';
7
import 'package:uuid/uuid.dart';
8
9
class AssetProfileScreen extends StatefulWidget {
10
@override
11
AssetProfileScreenState createState() => new AssetProfileScreenState();
12
}
13
14
class AssetProfileScreenState extends State<AssetProfileScreen> with ToastMixin {
15
final assetTracking = AssetTracking();
16
String assetId = "";
17
String assetDetailInfo = "";
18
19
@override
20
void initState() {
21
super.initState();
22
initAssetTracking();
23
}
24
25
void initAssetTracking() {
26
assetTracking.initialize(apiKey: accessKey);
27
}
28
29
@override
30
Widget build(BuildContext context) {
31
return Scaffold(
32
appBar: AppBar(
33
toolbarHeight: 0,
34
),
35
body: Container(
36
padding: EdgeInsets.all(16),
37
child: Column(
38
crossAxisAlignment: CrossAxisAlignment.start,
39
children: [
40
ElevatedButton(
41
onPressed: assetId.isEmpty
42
? () async {
43
AssetProfile profile = AssetProfile(
44
customId: const Uuid().v4().toString(),
45
name: "test asset",
46
description: "asset descriptions",
47
attributes: {});
48
AssetResult result = await assetTracking.createAsset(profile: profile);
49
if (result.success) {
50
showToast("Create asset successfully with asset id ${result.data}");
51
setState(() {
52
assetId = result.data;
53
});
54
} else {
55
showToast(result.msg.toString());
56
}
57
}
58
: null,
59
child: const Text("Create New Asset"),
60
),
61
ElevatedButton(
62
onPressed: assetId.isNotEmpty
63
? () async {
64
var assetResult = await assetTracking.bindAsset(customId: assetId);
65
if (assetResult.success) {
66
showToast("Bind asset successfully with asset id ${assetResult.data}");
67
} else {
68
showToast(assetResult.msg.toString());
69
}
70
}
71
: null,
72
child: const Text("Bind Asset"),
73
),
74
ElevatedButton(
75
onPressed: assetId.isNotEmpty
76
? () async {
77
var assetProfile = AssetProfile(
78
customId: assetId,
79
name: "new name",
80
description: "new description",
81
attributes: {"attribute1": "tester1"});
82
var assetDetail = await assetTracking.updateAsset(assetProfile: assetProfile);
83
if (assetDetail.success) {
84
showToast("Update Asset Info successfully");
85
} else {
86
showToast(assetDetail.msg.toString());
87
}
88
}
89
: null,
90
child: const Text("Update Asset Info"),
91
),
92
ElevatedButton(
93
onPressed: assetId.isNotEmpty
94
? () async {
95
var assetDetail = await assetTracking.getAssetDetail();
96
if (assetDetail.success) {
97
setState(() {
98
assetDetailInfo = jsonEncode(assetDetail.data);
99
});
100
} else {
101
showToast(assetDetail.msg.toString());
102
}
103
}
104
: null,
105
child: const Text("Get Asset Detail"),
106
),
107
Text(assetDetailInfo)
108
],
109
),
110
),
111
);
112
}
113
114
@override
115
void dispose() {
116
super.dispose();
117
}
118
}

Here are the code highlights for each of the four functionalities:

Create an Asset: assetTracking.createAsset(profile: profile)

  1. This functionality is implemented within the onPressed callback of the "Create New Asset" button.
  2. It creates a new AssetProfile object with a custom ID, name, description, and empty attributes.
  3. The createAsset method of the assetTracking object is called to create the asset.
  4. If the operation is successful (result.success), the asset ID is stored (assetId = result.data), and a success message is displayed. Otherwise, an error message is shown.

Bind an Asset ID: assetTracking.bindAsset(customId: assetId)

  1. This functionality is implemented within the onPressed callback of the "Bind Asset" button.
  2. It calls the bindAsset method of the assetTracking object to bind the current asset ID.
  3. If the operation is successful (assetResult.success), a success message is displayed. Otherwise, an error message is shown.

Update Current Asset Profile: assetTracking.updateAsset(assetProfile: assetProfile)

  1. This functionality is implemented within the onPressed callback of the "Update Asset Info" button.
  2. It creates a new AssetProfile object with updated information (name, description, and attributes).
  3. The updateAsset method of the assetTracking object is called to update the asset profile.
  4. If the operation is successful (assetDetail.success), a success message is displayed. Otherwise, an error message is shown.

Retrieve Asset Detail of Current Asset ID: assetTracking.getAssetDetail()

  1. This functionality is implemented within the onPressed callback of the "Get Asset Detail" button.
  2. It calls the getAssetDetail method of the assetTracking object to retrieve detailed information about the current asset.
  3. If the operation is successful (assetDetail.success), the retrieved information is stored in assetDetailInfo and displayed on the screen. Otherwise, an error message is shown.

© 2024 NextBillion.ai all rights reserved.