Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4133 pages
Flutter and app store releases

dart analyze flags dart:html and package:js as deprecated in a Flutter web or Dart web project

Your error

info - 'dart:html' is deprecated and shouldn't be used. Use package:web and dart:js_interop instead. - deprecated_member_use3 more lines it can show

Fix it fast

Most common cause, 1 of 4

DOM code imports dart:html

Add package:web and import it with a prefix to avoid name clashes.

Fix
dart pub add web

import 'package:web/web.dart' as web;

void main() {
  final el = web.document.querySelector('#status') as web.HTMLElement?;
  el?.textContent = 'ready';
  el?.onClick.listen((web.MouseEvent e) {});
  web.window.localStorage.setItem('k', 'v');
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

The Dart analyzer now marks dart:html, and package:js, as deprecated because they cannot compile to WebAssembly. Their replacements are package:web for DOM and browser APIs and dart:js_interop for calling JavaScript. Add web to pubspec.yaml, import it with a prefix, swap @JS declarations to dart:js_interop, and change conditional imports from dart.library.html to dart.library.js_interop.

  • Applies to Dart 3.12, package:web 1.1, js 0.7.2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

List every file that still uses the old libraries, and check which of your dependencies pull in the discontinued js package.

Run this first
grep -rlnE "dart:html|dart:js_util|dart:js'|package:js/" lib test
dart pub deps --style=compact | grep -E "^- js |js "

Then find the line you see in this list and open its fix:

  1. 1DOM code imports dart:htmlThe deprecated_member_use info sits on an import 'dart:html' line
  2. 2JavaScript calls use package:js or dart:js_utilThe info names package:js/js.dart, and dart pub get lists js as discontinued
  3. 3Conditional imports test for dart.library.htmlAn export or import uses if (dart.library.html) to choose the web implementation
  4. 4Code relies on is checks against DOM typesAfter the switch, a test like event.target is InputElement never matches or behaves differently

What does the error look like?

  • info - 'dart:html' is deprecated and shouldn't be used. Use package:web and dart:js_interop instead. - deprecated_member_use
  • info - 'package:js/js.dart' is deprecated and shouldn't be used. Use dart:js_interop instead. - deprecated_member_use
  • + js 0.7.2 (discontinued)
  • 1 package is discontinued.

Cause 1 of 4

DOM code imports dart:html

You seeThe deprecated_member_use info sits on an import 'dart:html' line

Check

Dart's migration guide calls dart:html and the other core web libraries deprecated and unsupported for Wasm. package:web exposes the same browser APIs with names taken from the Web IDL, so HTMLElement replaces Element and so on.

Check
grep -rn "import 'dart:html'" lib

Fix

Add package:web and import it with a prefix to avoid name clashes. Casts to the exact HTML type are often needed after querySelector.

Fix
dart pub add web

import 'package:web/web.dart' as web;

void main() {
  final el = web.document.querySelector('#status') as web.HTMLElement?;
  el?.textContent = 'ready';
  el?.onClick.listen((web.MouseEvent e) {});
  web.window.localStorage.setItem('k', 'v');
}

Verify

dart analyze reports no deprecated_member_use on the file.

Verify
dart analyze lib

Confirmed by dart.dev

Did this fix it?

Cause 2 of 4

JavaScript calls use package:js or dart:js_util

You seeThe info names package:js/js.dart, and dart pub get lists js as discontinued

Check

The old @JS annotation from package:js and the helpers in dart:js_util are replaced by static interop in dart:js_interop, which works for both the JavaScript and the Wasm compilers.

Check
grep -rnE "package:js/|dart:js_util" lib

Fix

Declare the JavaScript function with the @JS annotation from dart:js_interop and pass JS types, converting Dart values with toJS. Then remove js from pubspec.yaml.

Fix
import 'dart:js_interop';

@JS('console.log')
external void consoleLog(JSString message);

void ping() => consoleLog('clicked'.toJS);

Verify

After removing js from pubspec.yaml, pub get no longer reports a discontinued package from your own dependencies and analyze is clean.

Verify
dart pub get && dart analyze

Confirmed by dart.dev

Did this fix it?

Cause 3 of 4

Conditional imports test for dart.library.html

You seeAn export or import uses if (dart.library.html) to choose the web implementation

Check

That condition depends on the deprecated library. The migration guide says to tell web from native with dart.library.js_interop instead, which is also true for Wasm builds.

Check
grep -rn "dart.library.html" lib

Fix

Switch the condition and point it at a file written with package:web.

Fix
export 'src/storage_stub.dart'
    if (dart.library.io) 'src/storage_io.dart'
    if (dart.library.js_interop) 'src/storage_web.dart';

Verify

flutter build web, and flutter build web --wasm if you target Wasm, both compile.

Verify
flutter build web --wasm

Confirmed by dart.dev

Did this fix it?

Cause 4 of 4

Code relies on is checks against DOM types

You seeAfter the switch, a test like event.target is InputElement never matches or behaves differently

Check

package:web types are all JSObject at runtime, so an is check cannot tell an input from a div. The guide replaces such tests with isA or instanceOfString.

Check
grep -rnE " is (Html|Input|Anchor|Div)\w*Element" lib

Fix

Use the typed helper from dart:js_interop.

Fix
import 'dart:js_interop';
import 'package:web/web.dart' as web;

bool isTextInput(web.EventTarget? t) => t != null && t.isA<web.HTMLInputElement>();

Verify

Exercise the branch in a browser run; the check now returns true for the right element only.

Verify
flutter run -d chrome

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Write new web code against package:web from the first line.
  • Turn on the Wasm build in CI; it fails fast on anything that still needs dart:html.
  • Check pub get output for discontinued packages after each dependency update.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.