Skip to content

fix: never crash invoke handlers on nil or missing args - #52

Merged
AdamMusa merged 1 commit into
codex/engine-initializationfrom
fix/invoke-handler-null-args
Nov 2, 2026
Merged

AdamMusa merged 1 commit into
codex/engine-initializationfrom
fix/invoke-handler-null-args

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Engine-only fix. No Ruby change is needed.

Revised after QA review: genuinely required arguments now return a clear error reply instead of a silent no-op. Meaningful defaults are kept. This follows QA's 32-site classification in qa-reviews.md (engine#52).

Problem

Ruby's Page#invoke sends "args" => nil whenever a method is called without arguments, for example audio.play, page_view.next_page, interactive_viewer.reset or search_bar.open_view. Most Dart invoke handlers indexed args[...] unconditionally, so these calls threw NoSuchMethodError on the client. Several handlers also failed with cryptic errors when a key was missing, even with a map payload:

  • args["key"]! in SharedPreferences, SecureStorage and UrlLauncher
  • as int / as List in Cupertino Tabs and Clipboard
  • args["rtl"] ? ... in SemanticsService
  • Completer<bool>.complete(null) in View confirm_pop
  • int→double? and non-null String/bool parameters in Screenshot, Tester, FilePicker, WebView and Geolocator

Fix

  1. Root fix: Control.invokeMethod normalizes args with the new invokeArgs() (utils/invoke.dart, exported). Every listener in core and in every extension now always receives a Map.
  2. Required arguments: requireInvokeArg(args, "key", "SecureStorage.set") throws ArgumentError('SecureStorage.set requires a "key" argument'). The backend already returns a thrown handler error as the invoke error (ruflet_backend.dart _onInvokeMethod), so Ruby raises it (sync) or gets it in on_result (async). A missing required value can no longer look like success, and get(nil) can no longer look like "not found".
  3. Optional arguments keep a meaningful default. The no-op helper skipInvoke is now used only for Tabs move_to without an index.
Handler Missing argument → behavior
SecureStorage set / get / contains_key / remove error (key; value for set). A null value would make flutter_secure_storage delete the key, so it never reaches the platform.
SharedPreferences set / get / contains_key / remove error (key; value for set), raised before touching the plugin
SharedPreferences get_keys default: no prefix lists all keys
Clipboard set / set_image / set_files error (data / files; a non-list files is an error too)
Share share_text / share_uri / share_files error (text / uri / files; an empty file list is an error too)
UrlLauncher launch_url / can_launch_url / open_window error (url; a non-string url is an error too)
SemanticsService announce_message / announce_tooltip error (message); rtl defaults to false
ScreenBrightness set_* error (value), with a clearer message than the old ArgumentError.notNull
FilePicker save_file error (src_bytes on web/mobile, file_name on web)
FilePicker defaults: allow_multiple → false; upload with nothing to upload → no-op
Page push_route error (route)
Page set_allowed_device_orientations default: all orientations (a non-list is ignored)
View confirm_pop default: false, keep the view (a Completer<bool> cannot take null)
WebView load_file / load_html error (path / value)
Camera initialize error (description)
Tester find_by_*, take_screenshot, tap, tap_at, long_press, enter_text, mouse_hover error (the required finder, query, key, icon, name, offset or text)
Tester, any method with no harness attached StateError, so a broken test script cannot pass silently
Tester defaults: pump / pump_and_settle duration; finder_index → 0
Tabs move_to (Material and Cupertino) default: no index → no-op. The Material hunk is byte-identical to #18, so they merge in either order.
SearchBar close_view default: no text → close and keep the current query
Screenshot capture pixel_ratio parsed as a double (an int used to throw)
Geolocator distance_between coordinates parsed as doubles (ints used to throw)

Every other handler is safe once args is a map, because each uses null-safe parse* helpers or optional parameters:

  • Core: Button, IconButton, Dropdown(M2), Cupertino Dropdown and SearchBar, KeyboardListener, TextField, Canvas, Dismissible, InteractiveViewer, ScrollableControl, PageView, ContextMenu, Pagelet, Window.
  • Extensions: Audio, AudioRecorder, CodeEditor, Map, PermissionHandler, Video, Ads, Flashlight, QR scanner.

Unknown method names still throw. Two pre-existing use_super_parameters infos in ruflet_webview are also fixed.

Tests

packages/ruflet/test/widgets/invoke_handler_null_args_test.dart (42 tests):

  • Control.invokeMethod: hands listeners {} for null and for a non-map, and passes maps through.
  • Controls: every control handler is mounted on iOS and Android, and each invoke method is called with null and with {}. None throws, and each optional argument falls back to its default (Tabs, PageView, Column, InteractiveViewer, ContextMenu, SearchBar, Canvas, Dismissible, focus methods, Page orientations, View confirm_pop).
  • Required arguments: for 8 services (31 methods), a null or {} payload gives an ArgumentError with the exact message "<Service>.<method> requires a "<arg>" argument". Missing value in set, non-list Clipboard files, an empty Share file list and a non-string url each give a clear error. Page push_route gives an error.
  • Optional arguments: get_keys with no prefix returns all keys, FilePicker upload returns nil, and Tester pump / pump_and_settle / teardown succeed with a harness attached.
  • Tester without a harness: raises StateError("Tester.<method> requires a running test harness").
  • Through the real reply path: a fake RufletBackendChannel runs the backend's own dispatch, so the test sees exactly what Ruby receives:
    • SharedPreferences.set with args: nil → reply {result: nil, error: "...SharedPreferences.set requires a "key" argument"}.
    • get_keys with nil → {result: ["a"], error: nil}.
    • PageView.next_page with nil → no error, and the page turns.

packages/ruflet_secure_storage/test/secure_storage_test.dart (new, 2 tests; the method channel is mocked):

  • With a missing key, or set without value, no platform call is made: no write and no delete.
  • Complete calls reach the platform, with set coercing 5 to "5"; get_all needs no args.

verify_pr.sh with EXT_PKGS="ruflet_secure_storage ruflet_webview ruflet_geolocator ruflet_camera":

PASS engine:ruflet flutter test         +737: All tests passed!
PASS engine:ruflet analyze              No issues found!
PASS engine:ruflet_secure_storage flutter test +2: All tests passed!
PASS engine:ruflet_secure_storage analyze No issues found!
SKIP engine:ruflet_webview flutter test no test/ dir
PASS engine:ruflet_webview analyze      No issues found!
SKIP engine:ruflet_geolocator flutter test no test/ dir
PASS engine:ruflet_geolocator analyze   No issues found!
SKIP engine:ruflet_camera flutter test  no test/ dir
PASS engine:ruflet_camera analyze       No issues found!
RESULT: PASS

Not changed here

Invokes that no handler claims (Window on mobile/web, iOS ContextMenu open) are fixed generically by #59.

Ruby's Page#invoke sends args: nil for methods called without
arguments, and handlers indexed args[...] unconditionally, so calls such
as page_view.next_page or audio.play failed on the client.

Control.invokeMethod now always hands listeners a map (new invokeArgs
helper). Handlers that still crashed on a missing key now either use a
meaningful default (SharedPreferences get_keys prefix, Tabs move_to,
SearchBar close_view, View confirm_pop, device orientations, Tester
finder_index) or, when the argument is genuinely required, throw a clear
ArgumentError via requireInvokeArg (e.g. 'SecureStorage.set requires a
"value" argument'), which the backend returns to Ruby as the invoke
error instead of a cryptic null-check failure or a silent success.
Tester without a harness raises a StateError instead of passing.

Covers Tabs, SearchBar, Screenshot, View, Page, the Clipboard,
FilePicker, ScreenBrightness, SemanticsService, Share,
SharedPreferences, Tester and UrlLauncher services, and the
SecureStorage, WebView, Geolocator and Camera extensions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant