flutter test fails because a file imports dart:html
Error: 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.
Fix it fast
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/2Match what you see, then jump to its fix
- 1
The 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
Even after stubbing, the test needs a real window, document or storageThe code under test only works in a browserFix - 3
The app also fails to build for WebAssembly, or the analyzer flags dart:htmldart:html itself is the problem: it is deprecatedFix
- 1
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 testConfirmed by dart.dev
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=chrome3Verify
The web-only tests now load and run in the browser.
flutter test --platform=chrome test/storage_web_test.dartConfirmed by github.com
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 testConfirmed by dart.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
Flutter web request fails with a CORS error and Dart code cannot fix it
CORS preflight channel did not succeed
In a browser, the browser decides whether your Dart code may read a cross-origin response, and the server grants that with CORS headers.
4 causes, each with a fixMobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fix