skip to Main Content

Error: Dart library ‘dart:ui’ is not available on this platform.
import ‘dart:ui’ show lerpDouble;

im trying to run a dummy flutter project and it shows me error it is not running properly i want it to run properly
erorr arise is
Error: Dart library ‘dart:ui’ is not available on this platform.
import ‘dart:ui’ show lerpDouble;

2

Answers


  1. The ‘dart:ui’ library can only be used when running code in a Flutter context. If you try to import it in other environments like a Dart command line app or web app, you’ll get an error that the library is not available.

    To fix this:

    Double check that you are running the code in a Flutter app or package by using flutter run or similar. Dart UI code won’t work outside of Flutter!

    Instead of importing dart:ui, you need to import the Flutter equivalent:

    import 'package:flutter/rendering.dart';
    

    Then access any UI capabilities like lerpDouble() through the Flutter libraries:

    final value = lerpDouble(a, b, t);
    

    The key points are:

    Flutter UI code must run in a Flutter environment
    Import package:flutter/rendering.dart instead of dart:ui
    Use the Flutter libraries to access UI functions
    Follow those steps and you should be able to fix the error! Let me know if you have any other questions and If my answer helps you don’t forget to vote 🙏️.

    Login or Signup to reply.
  2. In order to avoir this kind of issues when running on several platform, you can do this.

    Create a file to be imported where you need your widget(your_widget.dart):

    export 'YOUR_PACKAGE/YOUR_PATH/my_widget_common.dart'
        if (dart.library.io) 'YOUR_PACKAGE/YOUR_PATH/your_widget_mobile.dart'
        if (dart.library.html) 'YOUR_PACKAGE/YOUR_PATH/your_widget_web.dart'
        show YourWidget;
    

    Create a common file (your_widget_common.dart):

    class YourWidget {}
    

    And then create files by platform (your_widget_mobile.dart):

    import 'dart:ui';
    
    class YourWidget {
      /// implementation
    }
    

    (your_widget_web.dart):

    import 'dart:html';
    
    class YourWidget {
      /// implementation
    }
    

    Now, when calling your_widget.dart, you will automatically switch to the correct platform implementation of your widget.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search