From d859865e34e40219a29f7eecff12cb50d40489cb Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Thu, 16 Nov 2023 13:55:26 -0800 Subject: [PATCH] Enable the silent flag for invalid string exceptions when building a TextSpan (#138564) This error can occur in a release app (for example, if the text comes from user input that is not valid UTF-16). In that case, TextSpan will replace the invalid text with a placeholder character. --- packages/flutter/lib/src/painting/text_span.dart | 1 + packages/flutter/test/painting/text_painter_test.dart | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/painting/text_span.dart b/packages/flutter/lib/src/painting/text_span.dart index 634932ee6fc..d50bce8484e 100644 --- a/packages/flutter/lib/src/painting/text_span.dart +++ b/packages/flutter/lib/src/painting/text_span.dart @@ -285,6 +285,7 @@ class TextSpan extends InlineSpan implements HitTestTarget, MouseTrackerAnnotati stack: stack, library: 'painting library', context: ErrorDescription('while building a TextSpan'), + silent: true, )); // Use a Unicode replacement character as a substitute for invalid text. builder.addText('\uFFFD'); diff --git a/packages/flutter/test/painting/text_painter_test.dart b/packages/flutter/test/painting/text_painter_test.dart index a3ce3c1f26e..5d14a1f3b42 100644 --- a/packages/flutter/test/painting/text_painter_test.dart +++ b/packages/flutter/test/painting/text_painter_test.dart @@ -1193,9 +1193,9 @@ void main() { }, skip: isBrowser && !isCanvasKit); // https://github.com/flutter/flutter/issues/87543 test('TextPainter handles invalid UTF-16', () { - Object? exception; + FlutterErrorDetails? error; FlutterError.onError = (FlutterErrorDetails details) { - exception = details.exception; + error = details; }; final TextPainter painter = TextPainter() @@ -1207,7 +1207,8 @@ void main() { painter.layout(); // The layout should include one replacement character. expect(painter.width, equals(fontSize)); - expect(exception, isNotNull); + expect(error!.exception, isNotNull); + expect(error!.silent, isTrue); painter.dispose(); }, skip: kIsWeb); // https://github.com/flutter/flutter/issues/87544