Skip to content

Commit b1a28bc

Browse files
muradhossinmdebbar
andauthored
web_ui: avoid crash for showPerformanceOverlay; log 'not supported' once (flutter#173518)
Fixes flutter#172405 On Flutter Web, calling `MaterialApp(showPerformanceOverlay: true)` reaches `SceneBuilder.addPerformanceOverlay`, which previously threw `UnimplementedError` and crashed apps. This change makes the method a no-op on Web and logs a one-time warning: "showPerformanceOverlay is not supported on Flutter Web. Use DevTools Performance (Timeline) instead." Rationale: Avoid crashes and guide developers to the supported tooling on Web. Testing: - Relied on CI for web_ui builds and tests. - (Manual reproduction before fix) Enabling `showPerformanceOverlay` on Web produced UnimplementedError from `canvaskit/layer_scene_builder.dart`. --------- Co-authored-by: Mouad Debbar <mdebbar@google.com>
1 parent 57b7c46 commit b1a28bc

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

engine/src/flutter/lib/web_ui/lib/src/engine/layer/layer_scene_builder.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import 'dart:typed_data';
77
import 'package:ui/ui.dart' as ui;
88

99
import '../lazy_path.dart';
10+
import '../util.dart';
1011
import '../vector_math.dart';
1112
import 'layer.dart';
1213
import 'layer_painting.dart';
1314
import 'layer_tree.dart';
1415

16+
// Warn about performance overlay on Web only once per page load.
17+
bool _webPerfOverlayWarned = false;
18+
1519
class LayerScene implements ui.Scene {
1620
LayerScene(RootLayer rootLayer) : layerTree = LayerTree(rootLayer);
1721

@@ -43,8 +47,14 @@ class LayerSceneBuilder implements ui.SceneBuilder {
4347

4448
@override
4549
void addPerformanceOverlay(int enabledOptions, ui.Rect bounds) {
46-
// We don't plan to implement this on the web.
47-
throw UnimplementedError();
50+
// Not implemented on Web. Avoid crashing; warn once to guide developers.
51+
if (!_webPerfOverlayWarned) {
52+
_webPerfOverlayWarned = true;
53+
printWarning(
54+
'showPerformanceOverlay is not supported on Flutter Web. '
55+
'See: https://docs.flutter.dev/perf/web-performance',
56+
);
57+
}
4858
}
4959

5060
@override
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:test/bootstrap/browser.dart';
6+
import 'package:test/test.dart';
7+
import 'package:ui/src/engine.dart';
8+
import 'package:ui/ui.dart' as ui;
9+
10+
void main() {
11+
internalBootstrapBrowserTest(() => testMain);
12+
}
13+
14+
void testMain() {
15+
group('LayerSceneBuilder.addPerformanceOverlay', () {
16+
final List<String> warnings = <String>[];
17+
late void Function(String) oldPrintWarning;
18+
19+
setUpAll(() {
20+
oldPrintWarning = printWarning;
21+
printWarning = (String warning) {
22+
warnings.add(warning);
23+
};
24+
});
25+
26+
tearDownAll(() {
27+
printWarning = oldPrintWarning;
28+
});
29+
30+
test('does not throw and warns only once per page load', () {
31+
final sb1 = ui.SceneBuilder();
32+
warnings.clear();
33+
expect(() => sb1.addPerformanceOverlay(0, ui.Rect.zero), returnsNormally);
34+
expect(warnings, hasLength(1));
35+
expect(warnings.single, contains('showPerformanceOverlay is not supported on Flutter Web'));
36+
37+
final sb2 = ui.SceneBuilder();
38+
warnings.clear();
39+
expect(() => sb2.addPerformanceOverlay(0, ui.Rect.zero), returnsNormally);
40+
expect(warnings, isEmpty, reason: 'Second call should not produce additional warnings.');
41+
});
42+
});
43+
}

0 commit comments

Comments
 (0)