Files
smooth-app/packages/smooth_app/lib/data_models/downloadable_string.dart
Edouard Marquez e3bc40fdf3 chore: Migration to Dart 3.8 (#6668)
* Migration to Dart 3.8

* New GA

* Fix dartdoc
2025-06-23 18:14:17 +02:00

39 lines
1.0 KiB
Dart

import 'package:http/http.dart';
import 'package:smooth_app/database/dao_string.dart';
/// Downloadable String that may be stored (and compared to the previous value).
class DownloadableString {
DownloadableString(this.uri, {this.dao});
final Uri uri;
final DaoString? dao;
String? _value;
/// The actual string value.
String? get value => _value;
/// Downloads data and stores it locally if possible.
///
/// Returns true if the downloaded string is different
/// from the previously stored one.
/// May throw an Exception.
Future<bool> download() async {
final Response response = await get(uri);
if (response.statusCode != 200) {
throw Exception('status is ${response.statusCode} for $uri');
}
_value = response.body;
if (dao != null) {
final String key = uri.toString();
final String? previousString = await dao!.get(key);
if (_value == previousString) {
return false;
}
await dao!.put(key, _value);
}
return true;
}
}