Skip to content

Commit

Permalink
fix: custom snapshot should propagate exception from renderbox (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp committed Mar 27, 2023
1 parent c3035f6 commit be185cb
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions super_native_extensions/lib/src/widgets/snapshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,28 @@ class _PendingSnapshot {
final Offset location;
final completers = <Completer<TargetedImage?>>[];

void complete(TargetedImage? image) {
for (final completer in completers) {
completer.complete(image);
void complete(Future<TargetedImage?> image) async {
// Weirdly simply calling completer.complete(image) will resolve the future
// synchronously, which is not expected and may result in another
// getSnapshot() call in the meanwhile thus concurrent modification exception.
try {
final value = await image;
for (final completer in completers) {
completer.complete(value);
}
} catch (e, st) {
for (final completer in completers) {
completer.completeError(e, st);
}
}
}
}

TargetedImage _getSnapshot(
Future<TargetedImage> _getSnapshot(
BuildContext context,
RenderRepaintBoundary renderObject,
Offset location,
Offset Function(Rect rect, Offset offset)? translation) {
Offset Function(Rect rect, Offset offset)? translation) async {
final image = renderObject.toImageSync(
pixelRatio: MediaQuery.of(context).devicePixelRatio);
final transform = renderObject.getTransformTo(null);
Expand Down Expand Up @@ -261,7 +271,7 @@ class _CustomSnapshotWidgetState extends State<CustomSnapshotWidget>
}
if (!mounted) {
for (final snapshot in _pendingSnapshots) {
snapshot.complete(null);
snapshot.complete(Future.value(null));
}
_pendingSnapshots.clear();
return;
Expand Down Expand Up @@ -300,7 +310,7 @@ class _CustomSnapshotWidgetState extends State<CustomSnapshotWidget>
translation,
));
} else {
s.complete(null);
s.complete(Future.value(null));
}
}
_pendingSnapshots.clear();
Expand Down Expand Up @@ -378,7 +388,7 @@ class _FallbackSnapshotWidgetState extends State<FallbackSnapshotWidget>
void _checkSnapshot() {
if (!mounted) {
for (final snapshot in _pendingSnapshots) {
snapshot.complete(null);
snapshot.complete(Future.value(null));
}
_pendingSnapshots.clear();
return;
Expand Down

0 comments on commit be185cb

Please sign in to comment.