Maaz Zindani

Loading the index…

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

flutter test fails because a file imports dart:html

ErrorError: Not found: 'dart:html'

Short answer

flutter test runs on the Dart VM by default, and the VM has no browser libraries, so any file that imports dart:html fails to compile. Either hide the web code behind a conditional import so the VM gets a stub, or run the web-only tests with flutter test --platform chrome. For new code, use package:web.

  • Applies to Flutter 3.35, Dart 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every file that imports a browser library. Those are the files the VM cannot compile.

    grep -rn "dart:html\|dart:js_interop\|package:web" lib/ test/
  2. 2Match what you see, then jump to its fix

    1. 1The failing import is in a file the test pulls in, not in the test itselfShared code imports dart:html directly and the test runs on the VMFix
    2. 2Even after stubbing, the test needs a real window, document or storageThe code under test only works in a browserFix
    3. 3The app also fails to build for WebAssembly, or the analyzer flags dart:htmldart:html itself is the problem: it is deprecatedFix

What does the error look like?

  • Error: Not found: 'dart:html'

Cause 1 of 3

Shared code imports dart:html directly and the test runs on the VM

1Check

The test runner defaults to the vm platform. A library that imports dart:html is a web-only library, so the VM cannot load it, even if the app itself is fine in a browser.

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

2Fix

Put the web code in its own file and choose the file with a conditional import. The first path is the fallback that the VM gets; each if clause switches to another file when that library exists. All files must expose the same API.

// lib/src/storage.dart
export 'storage_stub.dart'
    if (dart.library.js_interop) 'storage_web.dart';

// lib/src/storage_stub.dart
String readToken() => throw UnsupportedError('readToken');

3Verify

Run the tests on the VM again. The import error is gone.

flutter test

Confirmed by dart.dev

Did this fix it?

Cause 2 of 3

The code under test only works in a browser

1Check

A stub on the VM cannot test browser behaviour. For packages meant for the web, the Flutter repository's plugin test guide says unit tests for web packages must run on Chrome.

flutter test --help | grep -n "platform"

2Fix

Run those tests on the chrome platform instead of the VM.

flutter test --platform=chrome

3Verify

The web-only tests now load and run in the browser.

flutter test --platform=chrome test/storage_web_test.dart

Confirmed by github.com

Did this fix it?

Cause 3 of 3

dart:html itself is the problem: it is deprecated

1Check

The Dart docs say dart:html and the other core web libraries are deprecated and not supported when compiling to Wasm, and name package:web as the replacement.

grep -rln "dart:html" lib/

2Fix

Add package:web, swap the import, and let dart fix rename what changed. Keep it behind the conditional import above so the VM still sees the stub.

dart pub add web
dart fix --dry-run
# then: import 'package:web/web.dart' as web;

3Verify

Analyze the project and run the tests on the VM and on Chrome.

flutter analyze
flutter test

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Keep browser-only code in files that only the web side of a conditional import reaches.
  • Test the logic through the stub-friendly interface, and keep a small set of tests marked for Chrome.
  • Start new web code on package:web, not dart:html.

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:

Related fixes

Next step

Want this handled for good?

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