Skip to content

Commit b023a57

Browse files
authored
Fix debugPrint(null) to not crash (flutter#24942)
1 parent 4659916 commit b023a57

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/flutter/lib/src/foundation/print.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ void debugPrintSynchronously(String message, { int wrapWidth }) {
4141
/// Implementation of [debugPrint] that throttles messages. This avoids dropping
4242
/// messages on platforms that rate-limit their logging (for example, Android).
4343
void debugPrintThrottled(String message, { int wrapWidth }) {
44+
final List<String> messageLines = message?.split('\n') ?? <String>['null'];
4445
if (wrapWidth != null) {
45-
_debugPrintBuffer.addAll(message.split('\n').expand<String>((String line) => debugWordWrap(line, wrapWidth)));
46+
_debugPrintBuffer.addAll(messageLines.expand<String>((String line) => debugWordWrap(line, wrapWidth)));
4647
} else {
47-
_debugPrintBuffer.addAll(message.split('\n'));
48+
_debugPrintBuffer.addAll(messageLines);
4849
}
4950
if (!_debugPrintScheduled)
5051
_debugPrintTask();
@@ -87,6 +88,7 @@ Future<void> get debugPrintDone => _debugPrintCompleter?.future ?? Future<void>.
8788

8889
final RegExp _indentPattern = RegExp('^ *(?:[-+*] |[0-9]+[.):] )?');
8990
enum _WordWrapParseMode { inSpace, inWord, atBreak }
91+
9092
/// Wraps the given string at the given width.
9193
///
9294
/// Wrapping occurs at space characters (U+0020). Lines that start with an

packages/flutter/test/foundation/print_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,16 @@ void main() {
5757
expect(log.length, 2);
5858
});
5959
});
60+
61+
test('debugPrint can print null', () {
62+
expect(
63+
captureOutput(() { debugPrintThrottled(null); }),
64+
equals(<String>['null']),
65+
);
66+
67+
expect(
68+
captureOutput(() { debugPrintThrottled(null, wrapWidth: 80); }),
69+
equals(<String>['null']),
70+
);
71+
});
6072
}

0 commit comments

Comments
 (0)