Skip to content
Snippets Groups Projects

Clhp map

Merged Sara Savanovic Djordjevic requested to merge clhp_map into main
13 files
+ 158
54
Compare changes
  • Side-by-side
  • Inline
Files
13
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import '../consts.dart';
import 'dart:typed_data';
import 'package:path_provider/path_provider.dart';
import '../consts.dart';
/// Fetch relation data from server
Future<Uint8List> fetchRelation() async {
@@ -21,16 +23,38 @@ Future<Uint8List> fetchRelation() async {
var responseBody = await response.transform(utf8.decoder).join();
if (responseBody.isNotEmpty) {
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
String filePath = '${appDocumentsDirectory.path}/last_relation.json';
try { // Write most recent time of update to file
await File(filePath).writeAsString(responseBody, mode: FileMode.write);
print('Relation written to file');
} catch (error) { print('Error in writing to file: $error');}
// Return relation data from the response body
return Uint8List.fromList(utf8.encode(responseBody));
} else {
throw Exception('Response body is empty');
}
} else {
throw Exception('Failed to fetch relation data: Status code ${response.statusCode}');
}
return loadSavedRelation();
} catch (e) {
throw Exception('Failed to fetch relation data: ${e.toString()}');
return loadSavedRelation();
}
}
Future<Uint8List> loadSavedRelation() async {
// Get latest saved relation from file if the server does not respond
Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
String filePath = '${appDocumentsDirectory.path}/last_relation.json';
// Read file contents
File file = File(filePath);
if (await file.exists()) {
String contents = await file.readAsString();
List<dynamic> jsonData = json.decode(contents); // Parse JSON string from file
Uint8List relation = Uint8List.fromList(utf8.encode(jsonData.toString()));
return relation;
} else {
throw Exception('File does not exist');
}
}
Loading