From 932d4663fd25c3648739c29e0c63218c08c52474 Mon Sep 17 00:00:00 2001 From: jezell Date: Mon, 26 Aug 2024 11:43:03 -0700 Subject: [PATCH] Handle null child query (#2151) * resolve todo about null child query (can cause exceptions occasionally when childQuery returns null) * gaurd against null child query in collectStyles --------- Co-authored-by: Jesse Ezell --- lib/src/document/document.dart | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/src/document/document.dart b/lib/src/document/document.dart index 457508ed8..9ff917bba 100644 --- a/lib/src/document/document.dart +++ b/lib/src/document/document.dart @@ -182,6 +182,9 @@ class Document { /// Special case of no-selection at start of empty line: gets inline style(s) from preceding non-empty line. Style collectStyle(int index, int len) { var res = queryChild(index); + if (res.node == null) { + return const Style(); + } if (len > 0) { return (res.node as Line).collectStyle(res.offset, len); } @@ -267,11 +270,13 @@ class Document { ChildQuery queryChild(int offset) { // TODO: prevent user from moving caret after last line-break. final res = _root.queryChild(offset, true); + if (res.node == null) { + return res; + } if (res.node is Line) { return res; } - final block = res.node - as Block; // TODO: Can be nullable, handle this case to avoid cast exception + final block = res.node as Block; return block.queryChild(res.offset, true); }