load method
Implementation
@override
Future<Avatar>load({required String id, void Function(double progress)? onProgress}) async {
final random = Random().nextInt(1000000000);
final eventID = '${id}_${DateTime.now().millisecondsSinceEpoch}_$random';
StreamSubscription? subscription;
if (onProgress != null) {
subscription = _eventChannel.receiveBroadcastStream(eventID).listen((progress) {
onProgress(progress as double);
});
}
try {
final result = await _methodChannel.invokeMethod('load', {'id': id, 'eventID': eventID});
return Avatar.fromJson(Map<String, dynamic>.from(result));
} on PlatformException catch (e) {
final error = AvatarError.fromNameOrNull(e.code);
if (error != null) {
throw error;
} else {
rethrow;
}
} finally {
try {
if (subscription != null) {
await subscription.cancel();
}
} catch (_) {
// ignore cancel errors
}
}
}