Skip to content

Commit a97dd30

Browse files
authored
Fix sample errors (#8)
* Fix warnings * Check unzipped directory doesn't already exist to avoid error * Update apply_scheduled_updates_to_preplanned_map_area_sample.dart * Update display_points_using_clustering_feature_reduction_sample.dart * Update find_closest_facility_sample.dart * Update generate_route_with_directions_sample.dart * Update suggest_address_sample.dart
1 parent ab397cb commit a97dd30

6 files changed

+56
-36
lines changed

lib/src/sample_data.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Future<void> downloadSampleData(List<String> portalItemIds) async {
2828

2929
for (final itemId in portalItemIds) {
3030
// create a portal item to ensure it exists and load to access properties
31-
var portalItem =
31+
final portalItem =
3232
PortalItem.withUri(Uri.parse('$portal/home/item.html?id=$itemId'));
3333
if (portalItem != null) {
3434
await portalItem.load();
@@ -42,10 +42,14 @@ Future<void> downloadSampleData(List<String> portalItemIds) async {
4242
// if the data is a zip we need to extract it
4343
// save all files to the device app directory in a directory with the item name without the zip extension
4444
final nameWithoutExt = itemName.replaceFirst(RegExp(r'.zip$'), '');
45-
final dir = Directory.fromUri(Uri.parse('$appDirPath/$nameWithoutExt'));
46-
await ZipFile.extractToDirectory(zipFile: file, destinationDir: dir);
47-
// clean up the zip folder now that the data has been extracted
48-
await file.delete();
45+
if (!Directory.fromUri(Uri.parse('$appDirPath/$nameWithoutExt'))
46+
.existsSync()) {
47+
final dir =
48+
Directory.fromUri(Uri.parse('$appDirPath/$nameWithoutExt'));
49+
await ZipFile.extractToDirectory(zipFile: file, destinationDir: dir);
50+
// clean up the zip folder now that the data has been extracted
51+
await file.delete();
52+
}
4953
}
5054
}
5155
}

lib/src/samples/apply_scheduled_updates_to_preplanned_map_area_sample.dart

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,19 @@ class ApplyScheduledUpdatesToPreplannedMapAreaState
108108
try {
109109
await _mobileMapPackage!.load();
110110
} catch (err) {
111-
showAlertDialog(
112-
'Mobile Map Package failed to load with error: {$err}',
113-
title: 'Error',
114-
);
111+
if (mounted) {
112+
showAlertDialog(
113+
'Mobile Map Package failed to load with error: {$err}',
114+
title: 'Error',
115+
);
116+
}
115117
return false;
116118
}
117119

118120
if (_mobileMapPackage!.maps.isEmpty) {
119-
showAlertDialog('Mobile map package contains no maps.');
121+
if (mounted) {
122+
showAlertDialog('Mobile map package contains no maps.');
123+
}
120124
return false;
121125
}
122126

@@ -138,13 +142,14 @@ class ApplyScheduledUpdatesToPreplannedMapAreaState
138142
Future<void> checkForUpdates() async {
139143
if (!_isInitialized) return;
140144
final updatesInfo = await _offlineMapSyncTask!.checkForUpdates();
141-
142-
setState(() {
143-
_updateStatus = updatesInfo.downloadAvailability;
144-
_updateSizeKB = updatesInfo.scheduledUpdatesDownloadSize / 1024;
145-
_canUpdate = updatesInfo.downloadAvailability ==
146-
OfflineUpdateAvailability.available;
147-
});
145+
if (mounted) {
146+
setState(() {
147+
_updateStatus = updatesInfo.downloadAvailability;
148+
_updateSizeKB = updatesInfo.scheduledUpdatesDownloadSize / 1024;
149+
_canUpdate = updatesInfo.downloadAvailability ==
150+
OfflineUpdateAvailability.available;
151+
});
152+
}
148153
}
149154

150155
void syncUpdates() {
@@ -164,10 +169,12 @@ class ApplyScheduledUpdatesToPreplannedMapAreaState
164169
// Refresh the update status
165170
checkForUpdates();
166171
}, onError: (err) {
167-
showAlertDialog(
168-
'The offline map sync failed with error: {$err}.',
169-
title: 'Error',
170-
);
172+
if (mounted) {
173+
showAlertDialog(
174+
'The offline map sync failed with error: {$err}.',
175+
title: 'Error',
176+
);
177+
}
171178
});
172179
}
173180

lib/src/samples/display_points_using_clustering_feature_reduction_sample.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class DisplayPointsUsingClusteringFeatureReductionSampleState
7070
void onMapViewReady() async {
7171
_mapViewController.arcGISMap = _map;
7272
await _map.load();
73-
if (_map.loadStatus == LoadStatus.loaded) {
73+
if (_map.loadStatus == LoadStatus.loaded && mounted) {
7474
setState(() => _ready = true);
7575
}
7676
}

lib/src/samples/find_closest_facility_sample.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ class FindClosestFacilitySampleState extends State<FindClosestFacilitySample> {
119119
_closestFacilityParameters = await generateClosestFacilityParameters(
120120
facilitiesLayer, incidentsLayer);
121121

122-
setState(() => _isInitialized = true);
122+
if (mounted) {
123+
setState(() => _isInitialized = true);
124+
}
123125
}
124126

125127
FeatureLayer buildFeatureLayer(Uri tableUri, Uri imageUri) {
@@ -177,8 +179,9 @@ class FindClosestFacilitySampleState extends State<FindClosestFacilitySample> {
177179
_routeGraphicsOverlay.graphics.add(routeGraphic);
178180
}
179181
}
180-
181-
setState(() => _isRouteSolved = true);
182+
if (mounted) {
183+
setState(() => _isRouteSolved = true);
184+
}
182185
}
183186

184187
void resetRoutes() {

lib/src/samples/generate_route_with_directions_sample.dart

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ class GenerateRouteWithDirectionsSampleState
9797
initMap();
9898
initStops();
9999
await initRouteParameters();
100-
101-
setState(() {
102-
_isReady = true;
103-
});
100+
if (mounted) {
101+
setState(() {
102+
_isReady = true;
103+
});
104+
}
104105
}
105106

106107
void initMap() {
@@ -176,7 +177,9 @@ class GenerateRouteWithDirectionsSampleState
176177
final routeResult =
177178
await _routeTask.solveRoute(routeParameters: _routeParameters);
178179
if (routeResult.routes.isEmpty) {
179-
showAlertDialog('No routes have been generated.', title: 'Info');
180+
if (mounted) {
181+
showAlertDialog('No routes have been generated.', title: 'Info');
182+
}
180183
return;
181184
}
182185

@@ -188,10 +191,12 @@ class GenerateRouteWithDirectionsSampleState
188191
_routeGraphicsOverlay.graphics.add(routeGraphic);
189192
}
190193

191-
setState(() {
192-
_directions = route.directionManeuvers;
193-
_isRouteGenerated = true;
194-
});
194+
if (mounted) {
195+
setState(() {
196+
_directions = route.directionManeuvers;
197+
_isRouteGenerated = true;
198+
});
199+
}
195200
}
196201

197202
Dialog showDirections(BuildContext context) {

lib/src/samples/suggest_address_sample.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,9 @@ class SuggestAddressSampleState extends State<SuggestAddressSample> {
172172

173173
final suggestResults = await _suggestOperation!.value;
174174
_suggestOperation = null;
175-
176-
setState(() => _suggestResults = List.from(suggestResults));
175+
if (mounted) {
176+
setState(() => _suggestResults = List.from(suggestResults));
177+
}
177178

178179
if (_suggestAgain != null) {
179180
// start again with the latest input value

0 commit comments

Comments
 (0)