The error “dart library ‘dart:html’ is not available on this platform” occurs when you try to use the ‘dart:html’ library in a Dart codebase that is not specifically intended to run in a web browser. The ‘dart:html’ library provides access to the HTML DOM API, which is only available in a browser environment.
To resolve this error, you need to make sure that you are running your Dart code in an appropriate environment that supports the ‘dart:html’ library. Typically, this means running the code in a web browser or using a Dart web framework like Flutter or AngularDart.
Here’s an example where the error occurs:
import 'dart:html'; void main() { // Use 'dart:html' API var element = querySelector('#myElement'); // ... }
In this example, the code tries to use the querySelector function from the ‘dart:html’ library to select an HTML element with the id ‘myElement’. However, if you try to run this code outside a browser environment (e.g., on the command line using Dart VM), you will encounter the mentioned error.
To use ‘dart:html’ in a browser environment, you can either run your Dart code in a web browser directly or utilize a web framework like Flutter or AngularDart that supports rendering Dart code in a browser.