installUpdate method
Implementation
Future<void> installUpdate(final String url, {final String? version}) async {
try {
final response = await dio.get(url);
final Directory tempDir = Directory.systemTemp;
final File tempFile = File(
'${tempDir.path}/${FlutterIgnite.title}${version != null &&
version.isNotEmpty ? version : ""}.zip');
// TODO: make sure the file is extracted, might be invalid data?
await tempFile.writeAsBytes(response.data);
// extract and replace the app (this part is platform-specific)
final bytes = tempFile.readAsBytesSync();
final archive = ZipDecoder().decodeBytes(bytes);
for (final ArchiveFile file in archive.files) {
final filename = file.name;
if (file.isFile) {
final data = file.content as List<int>;
File('${tempDir.path}/$filename')
..createSync(recursive: true)
..writeAsBytes(data);
}
}
// move extracted files to the app directory and restart the app
// TODO: make it current directory
// final appDir = Directory('/');
// TODO: make proper testing
final appDir = Directory.current;
for (final file in tempDir.listSync()) {
file.renameSync('${appDir.path}/${file.uri.pathSegments.last}');
}
// Restart the app (platform-specific implementation needed)
} catch (ex) {
Pen.error("Update install failed");
}
}