Skip to content

Conversation

ebraminio
Copy link

@ebraminio ebraminio commented Sep 18, 2025

According to https://dart.dev/interop/js-interop/past-js-interop we should now "prefer using dart:js_interop going forwards and migrate usages of old interop libraries when possible" and this change does that in order to make the library compatible with --wasm builds.

Submit a pull request

Linear: FLU-

Github Issue: #

CLA

Description of the pull request

When I'm building example of this project in flutter run -d chrome --wasm mode the web mode isn't recognized correctly and raises errors like this on the console

image

Which this change fixes

Summary by CodeRabbit

  • Bug Fixes

    • Fixed web build issues (including dart2wasm) to improve stability for web users.
  • Refactor

    • Standardized web platform detection and handling across core, attachment handling, and persistence layers.
  • Chores

    • Aligned web targeting with current Dart/Flutter tooling for broader browser compatibility.
    • No public API changes.

Copy link
Contributor

coderabbitai bot commented Sep 18, 2025

Walkthrough

Replaced conditional imports/exports from dart.library.html to dart.library.js_interop in platform detection, attachment handling, and persistence DB selection. No public APIs or signatures were changed; web implementation is now selected via JS interop availability.

Changes

Cohort / File(s) Summary of Changes
JS interop gating update
packages/stream_chat/lib/src/core/platform_detector/platform_detector.dart, packages/stream_chat_flutter/lib/src/attachment/handler/stream_attachment_handler.dart, packages/stream_chat_persistence/lib/src/db/shared/shared_db.dart
Switched conditional imports/exports from if (dart.library.html) to if (dart.library.js_interop) to gate web-specific implementations; no public API changes.
Changelog
packages/stream_chat/CHANGELOG.md
Added entry: “Fixed build in dart2wasm environment.”

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant App
    participant Selector as "Conditional selector\n(if dart.library.js_interop)"
    participant WebImpl as "Web implementation"
    participant IOImpl as "IO implementation"

    App->>Selector: Request platform-specific module
    alt dart.library.js_interop present
        Selector->>WebImpl: Load/export web module
        WebImpl-->>App: Web implementation available
    else
        Selector->>IOImpl: Load/export IO module
        IOImpl-->>App: IO implementation available
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • renefloor

Poem

I twitch my whiskers at the toggle’s hop,
From html to js_interop—soft flip, no flop.
Three files aligned in a tidy little swap,
No APIs nudged, just a platform stop.
Carrot-coded cheer, thump-thump—hop-pop! 🥕🐇

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly highlights the primary change—fixing the build in the dart2wasm environment—and aligns directly with the PR’s purpose without extraneous detail or ambiguity.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between aa9cbc3 and 03760f5.

📒 Files selected for processing (4)
  • packages/stream_chat/CHANGELOG.md (1 hunks)
  • packages/stream_chat/lib/src/core/platform_detector/platform_detector.dart (1 hunks)
  • packages/stream_chat_flutter/lib/src/attachment/handler/stream_attachment_handler.dart (1 hunks)
  • packages/stream_chat_persistence/lib/src/db/shared/shared_db.dart (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/stream_chat/CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/stream_chat_flutter/lib/src/attachment/handler/stream_attachment_handler.dart
  • packages/stream_chat/lib/src/core/platform_detector/platform_detector.dart
🔇 Additional comments (1)
packages/stream_chat_persistence/lib/src/db/shared/shared_db.dart (1)

3-3: Confirm minimum SDK supports dart.library.js_interop.

The new gate depends on the dart:js_interop library being present (introduced in Dart ≥ 3.1). If our package still advertises compatibility with older Flutter/Dart releases, this will surface as an analyzer error (Undefined name 'dart.library.js_interop'). Please double-check the environment.sdk constraints and update them (or document the bump) so consumers on older SDKs aren’t broken.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
packages/stream_chat/lib/src/core/platform_detector/platform_detector.dart (1)

2-2: Tiny doc hint for future readers.

Consider a short comment explaining why js_interop (not html) is the web selector (Wasm compatibility).

-    if (dart.library.js_interop) 'platform_detector_web.dart'
+    // Use js_interop as the web selector to support both JS and Wasm runtimes.
+    if (dart.library.js_interop) 'platform_detector_web.dart'
packages/stream_chat_flutter/lib/src/attachment/handler/stream_attachment_handler.dart (1)

2-3: *Gate change LGTM; consider renaming the selected file to _web.dart for clarity.

Since selection now keys off js_interop (Web JS/Wasm), stream_attachment_handler_html.dart is a misnomer. Optional rename to stream_attachment_handler_web.dart (if feasible).

-export 'stream_attachment_handler_base.dart'
-    if (dart.library.js_interop) 'stream_attachment_handler_html.dart'
+export 'stream_attachment_handler_base.dart'
+    if (dart.library.js_interop) 'stream_attachment_handler_web.dart'
     if (dart.library.io) 'stream_attachment_handler_io.dart'
     show StreamAttachmentHandler;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

Disabled knowledge base sources:

  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between ee78069 and 6d7ec2a.

📒 Files selected for processing (3)
  • packages/stream_chat/lib/src/core/platform_detector/platform_detector.dart (1 hunks)
  • packages/stream_chat_flutter/lib/src/attachment/handler/stream_attachment_handler.dart (1 hunks)
  • packages/stream_chat_persistence/lib/src/db/shared/shared_db.dart (1 hunks)
🔇 Additional comments (3)
packages/stream_chat/lib/src/core/platform_detector/platform_detector.dart (2)

2-3: Confirm platform_detector_web.dart avoids dart:html

Verification script produced no output; cannot confirm. Verify packages/stream_chat/lib/src/core/platform_detector/platform_detector_web.dart does not import 'dart:html' or 'package:html' and instead imports 'dart:js_interop' or 'package:web'.


2-2: SDK constraints already cover dart.library.js_interop

All package pubspecs use sdk: ^3.6.2 (Dart ≥3.6) and Flutter constraints where present are >=3.27.4; dart.library.js_interop is supported since Dart 3.1 / Flutter 3.22 — no changes required.

packages/stream_chat_flutter/lib/src/attachment/handler/stream_attachment_handler.dart (1)

2-2: stream_attachment_handler_html.dart — no dart:html imports (resolved)

packages/stream_chat_flutter/lib/src/attachment/handler/stream_attachment_handler_html.dart does not import 'dart:html' or reference DOM-only types (it only uses FilePicker/XFile APIs); repo-wide search found no dart:html/package:web/package:js usages and the conditional export (if (dart.library.js_interop) ...) is correct.

export 'unsupported_db.dart'
if (dart.library.io) 'native_db.dart' // implementation using dart:io
if (dart.library.html) 'web_db.dart';
if (dart.library.js_interop) 'web_db.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

🧩 Analysis chain

Good switch; confirm web_db.dart is Wasm‑safe (no dart:html or non‑Wasm APIs).

If web_db.dart still uses dart:html or html-only storage APIs, this gate will break Wasm. Ensure it uses the Wasm-friendly drift/web/wasm path.


🏁 Script executed:

#!/bin/bash
# Ensure persistence web impl avoids dart:html
rg -nC2 -e "^import\s+['\"]dart:html['\"]" packages/stream_chat_persistence/lib/src/db/shared/web_db.dart || true
# Grep for drift wasm/web connectors (adjust patterns to your impl)
rg -nC2 -e "drift.*wasm" -e "import\s+['\"][^'\"]*drift/web" packages/stream_chat_persistence/lib/src/db/shared/web_db.dart || true

Length of output: 273


Do not approve — web_db.dart is not Wasm-safe; implement a Wasm connector

packages/stream_chat_persistence/lib/src/db/shared/web_db.dart still imports 'package:drift/web.dart' and has "// TODO: Replace with WASM implementation"; this uses web-only APIs and will break the Wasm gate. Replace with a Wasm-friendly connector (e.g., drift/web/wasm) or a drift wasm implementation that avoids dart:html, then re-verify.

@ebraminio
Copy link
Author

@xsahil03x Running it via dart.library.web with flutter run -d chrome --wasm on stream-chat-flutter/packages/stream_chat_persistence/example raises the following on the console,

image

But running it with dart.library.js_interop makes it

image

(dart.library.js_interop also works with flutter run -d chrome)

If you want to test stream-chat-flutter/packages/stream_chat_persistence/example please make sure to have simolus3/drift#3657 and Alberto-Monteiro/video_thumbnail#9 locally also and direct the example to use the locally installation of them.

@xsahil03x
Copy link
Member

Thanks, I will soon test it locally. Maybe we want to wait for the drift PR to be released first before merging this one

@ebraminio
Copy link
Author

ebraminio commented Sep 18, 2025

Thanks. Just to note it's not blocked to drift's PR (which is merged now but I understand you said a release is needed not just the merge) as it's possible to test stream-chat-flutter/packages/stream_chat/example one which doesn't have drift but that one needs flutter create . --platforms web to be initiated first but needs Alberto-Monteiro/video_thumbnail#9 anyway if I'm not mistaken.

And drift's PR isn't about js_interop but about use of int in JS context which apparently dart2wasm doesn't like it.

(and I don't know whether this is the best solution or not and am not pushing for it on any cost just that found this solution from https://dart.dev/interop/js-interop/past-js-interop (but inconsistently?) and found to be useful for the case here)

Copy link

codecov bot commented Sep 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.76%. Comparing base (ee78069) to head (6d7ec2a).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2379   +/-   ##
=======================================
  Coverage   63.76%   63.76%           
=======================================
  Files         412      412           
  Lines       25821    25821           
=======================================
  Hits        16466    16466           
  Misses       9355     9355           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ebraminio ebraminio changed the title Use updated import condition for web environment fix(core): Use updated import condition for web environment Sep 25, 2025
@ebraminio ebraminio changed the title fix(core): Use updated import condition for web environment fix(llc): Use updated import condition for web environment Sep 25, 2025
@ebraminio ebraminio changed the title fix(llc): Use updated import condition for web environment fix(llc): Fixed build in dart2wasm environment Sep 26, 2025
According to https://dart.dev/interop/js-interop/past-js-interop
we should now "prefer using dart:js_interop going forwards and
migrate usages of old interop libraries when possible" and this
change does that in order to make the library compatible with
`--wasm` builds.
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.

2 participants