getAvatar method
Gets the current user's avatar image, with local caching.
Returns a File pointing to the cached avatar. Use with Image.file().
Returns null if not logged in, user has no avatar, or network is
unavailable and no cached file exists.
Implementation
Future<File?> getAvatar() async {
final user = await _database.select(_database.users).getSingleOrNull();
if (user == null || user.avatarFilename.isEmpty) {
return null;
}
final filename = user.avatarFilename;
final cacheDir = await getApplicationCacheDirectory();
final file = File('${cacheDir.path}/avatars/$filename');
if (await file.exists()) {
return file;
}
try {
final bytes = await withAuth(() => _portalService.getAvatar(filename));
await file.parent.create(recursive: true);
await file.writeAsBytes(bytes);
if (!await _isDecodableImage(bytes)) {
await file.delete();
return null;
}
return file;
} on DioException {
return null;
}
}