How to Resolve Flutter Web Unit Testing Errors: Not Found: ‘dart:html’?
When running a flutter web application user may get some random error that says dart.html not found. So in today’s article, we will go through how to Resolve Flutter Web Unit Testing Errors: Not Found: ‘dart:html’.
As Flutter continues to evolve and expand its capabilities, developers are increasingly utilizing its web support to build powerful and responsive web applications. However, when it comes to unit testing these Flutter web projects, you might encounter a common error: “Not Found: ‘dart:html’.” In this blog post, we’ll tackle this specific issue head-on and explore effective solutions to resolve it. We’ll dive into the root causes behind the error, understand why the ‘dart:html’ package is unavailable during unit tests for Flutter web, and unveil step-by-step techniques to overcome this obstacle.
How to Resolve Flutter Web Unit Testing Errors: Not Found: ‘dart:html’ ??
Why does this error occur?
The analyzer produces this diagnostic when an import isn’t needed because none of the names that are imported are referenced within the importing library.
The problem comes because “Unit tests do not have access to dart:html. Unless you run them in the browser.”
From pub.dev/packages/test:
“By default, tests are run in the Dart VM, but you can run them in the browser as well by passing pub run test -p chrome path/to/test.dart. the test will take care of starting the browser and loading the tests, and all the results will be reported on the command line just like for VM tests.
In fact, you can even run tests on both platforms with a single command: pub run test -p “chrome, VM” path/to/test.dart”
To solve my particular problem I ran the tests using pub run test test/print_reports_card_test.dart -p chrome
Since you are running flutter tests for the web that use the dart:html package you must target the correct platform.
flutter test --platform chrome
Change import ‘dart:html‘; to import ‘package:http/http.dart’ as http;
and then change pubspec.yaml files dependencies to
dependencies: flutter: sdk: flutter http:
and click
pub get
Examples:
The following code produces this diagnostic because nothing defined in
dart:async
is referenced in the library:
import 'dart:async';
void main() {}
Common fixes:
If the import isn’t needed, then remove it.
If some of the imported names are intended to be used, then add the missing code.
Conclusion:
In this article, we have been through how to resolve flutter web unit testing errors: not found: ‘dart:html’.
Keep Learning !!! Keep Fluttering !!!
Do let us know in the comments if you are still confused in flutter!!
FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc.
FlutterAgency.com is one of the most popular online portals dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.