'How to solved 'dart:ui' error when going to run flutter app?

enter image description hereBelow error occurs when I try to run my first flutter app.

file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/cupertino/action_sheet.dart:5:8: Error: Not found: 'dart:ui'
import 'dart:ui' show ImageFilter;
       ^
 file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/cupertino/bottom_tab_bar.dart:5:8: Error: Not found: 'dart:ui'
import 'dart:ui' show ImageFilter;
       ^
file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/cupertino/colors.dart:5:8: Error: Not found: 'dart:ui'
import 'dart:ui' show Color;
       ^
file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/cupertino/dialog.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' show ImageFilter;
       ^
 file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/cupertino/nav_bar.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' show ImageFilter;
       ^
 file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/cupertino/slider.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' show lerpDouble;
       ^
 file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/cupertino/switch.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' show lerpDouble;
       ^
 file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/material/animated_icons.dart:9:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show Paint, Path, Canvas;
       ^   file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/material/animated_icons.dart:10:8: Error: Not found: 'dart:ui'
import 'dart:ui' show lerpDouble;
       ^  file:///Volumes/Data/FlutterSDk/flutter/packages/flutter/lib/src/material/arc.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' show lerpDouble;
       ^

Process finished with exit code 254

I have tried many ways to solve this issue but still, this issue is coming.



Solution 1:[1]

You are getting those errors because your IDE is executing main.dart as a simple dart application and not as a Flutter application.

As answered by salihguler, if you are using Android SDK then choose the main.dart file with Flutter Icon beside it and not the one with Dart Icon and the project should work just fine.

Android SDK Option Image

If you are using VS Code then instead of hitting Run (Ctrl+Alt+N) go to Debug -> Start Debugging option or simply press F5 and errors will be gone.

Visual Code Option Image

Solution 2:[2]

For me the culprit was a transitive dependency on Flutter. I was running a dart file that imported a functional class that I copied from another project. This class did not apparently depend on Flutter but it was importing a package for logging and that package was depending on Flutter.

When running a dart executable, make sure you do not use any Flutter specific classes anywhere.

Solution 3:[3]

If you are using VSCode, you might need to open Folder more closely to your main.dart.

Bad Folder Structure Example on the left side:

  • Backend
  • Frontend
    • FlutterApp
    • lib
      • main.dart

Working:

  • FlutterApp
    • lib
      • main.dart

Finally, I saved workplace as second one, and changed to first one, all works! Here is the related issue discussion.

Solution 4:[4]

had the same error message when working on command line based dart files, i had accidentally

imported cupertino packages on some dart files. try using material design only on your project and remove the cupertino package import on your files P.S i had no issue working on the real flutter app. and i use android studio

Solution 5:[5]

I was having the same problem, but pressing "F5 running on debug mode" worked for me. Additionally I don't need to install XCode and Android Studio as well, which was described as a problem in flutter doctor command.

Solution 6:[6]

If you're on VS code and are using extensions to run the code, don't. You should disable them and use the flutter extension it self to launch the main.dart.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nimantha
Solution 2 ToniTornado
Solution 3 Tokenyet
Solution 4 Aelaf
Solution 5 Jay Dangar
Solution 6 ouflak