diff --git a/lib/web_ui/lib/src/engine/html/recording_canvas.dart b/lib/web_ui/lib/src/engine/html/recording_canvas.dart
index a7c994990ad5d..55564b5fbfcaf 100644
--- a/lib/web_ui/lib/src/engine/html/recording_canvas.dart
+++ b/lib/web_ui/lib/src/engine/html/recording_canvas.dart
@@ -227,7 +227,7 @@ class RecordingCanvas {
}
void restore() {
- if (!_recordingEnded) {
+ if (!_recordingEnded && _saveCount > 1) {
_paintBounds.restoreTransformsAndClip();
}
if (_commands.isNotEmpty && _commands.last is PaintSave) {
diff --git a/lib/web_ui/test/engine/recording_canvas_test.dart b/lib/web_ui/test/engine/recording_canvas_test.dart
index 7bd1a9810eef4..e18309bae51f5 100644
--- a/lib/web_ui/test/engine/recording_canvas_test.dart
+++ b/lib/web_ui/test/engine/recording_canvas_test.dart
@@ -200,6 +200,13 @@ void testMain() {
// Should not throw exception on restore.
expect(() => rc.restore(), returnsNormally);
});
+
+ // Regression test for https://github.com/flutter/flutter/issues/61697.
+ test('Allows restore calls even if recording is not ended', () {
+ final RecordingCanvas rc = RecordingCanvas(Rect.fromLTRB(0, 0, 200, 400));
+ // Should not throw exception on restore.
+ expect(() => rc.restore(), returnsNormally);
+ });
}
// Expect a drawDRRect call to be registered in the mock call log, with the expectedArguments