`ArgumentError` in `_findCloseNode` when tapping an empty EditorState
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 84/100
Research direction
Start in lib/src/editor/editor_component/service/selection/shared.dart, focusing on getNodeInOffset and its call to _findCloseNode. Reproduce the tap with Document.blank(withInitialText: false), then verify the empty-node case returns null and the editor no longer throws an ArgumentError during selection handling.
Written by the indexing model from the issue text.
Description
Describe the bug
When EditorState is initialized with a completely empty Document (i.e. containing 0 nodes, such as Document.blank(withInitialText: false)) and the user taps inside the editor, an ArgumentError: Invalid argument(s): 0 is thrown by _findCloseNode in shared.dart.
Steps to Reproduce
- Initialize
EditorStatewithDocument.blank(withInitialText: false). - Render
AppFlowyEditor. - Tap on the editor surface.
- The application throws the following exception:
ArgumentError: Invalid argument(s): 0
Stack trace:
EditorStateSelection._findCloseNode
EditorStateSelection.getNodeInOffset
_MobileSelectionServiceWidgetState.getNodeInOffset
_MobileSelectionServiceWidgetState.getPositionInOffset
_MobileSelectionServiceWidgetState._onTapUpAndroid
Expected Behavior
The editor should handle the empty node list gracefully without throwing an exception, likely by returning null from getNodeInOffset and safely ignoring the tap selection.
Root Cause Analysis
In lib/src/editor/editor_component/service/selection/shared.dart, the getNodeInOffset function attempts to guard against invalid bounds:
if (start < 0 && end >= sortedNodes.length) {
return null;
}
If sortedNodes is empty, getNodeInOffset receives start = 0 and end = -1.
The condition end >= sortedNodes.length evaluates to -1 >= 0, which is false. Because of the && operator, the bounds check fails to catch the empty array case.
The code then proceeds to call _findCloseNode(sortedNodes, 0, -1).
Inside _findCloseNode, the binary search loop is skipped, and it returns:
return min.clamp(start, end);
Since min = 0, start = 0, and end = -1, 0.clamp(0, -1) throws an ArgumentError: Invalid argument(s): 0 because the lower bound is greater than the upper bound.
Proposed Fix
Change the bounds check in getNodeInOffset to:
if (start < 0 || end >= sortedNodes.length || start > end) {
return null;
}
Or alternatively, add an explicit check for empty lists:
if (sortedNodes.isEmpty) return null;
- Dominant language
- Dart
- Stars
- 684
- Forks
- 330
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from AppFlowy-IO/appflowy-editor
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
AppFlowy-IO/appflowy-editor#1204 · 4 comments · 11 reactions ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
AppFlowy-IO/appflowy-editor#1180 · 5 comments · 2 reactions ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
AppFlowy-IO/appflowy-editor#1151 · 1 reaction ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
AppFlowy-IO/appflowy-editor#1217 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 65/100
AppFlowy-IO/appflowy-editor#1215 ·
All issues in AppFlowy-IO/appflowy-editor
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
code quality good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Suwayomi/Suwayomi-Tsumiru#479 ·
-
Build Failure: agora_rtc_engine compiled against android-31 while dependencies require android-34+ Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
conceptadev/noir#95 ·