Not found: 'dart:ui' in dart scripts (not Flutter) - dart

I've got a simple Dart script (not Flutter) which needs to use the dart:ui core library, e.g.
import 'dart:ui';
void main() {
const color = Color(0xFF9585E5);
print(color);
}
From the get dart docs:
Note: The Flutter SDK includes the full Dart SDK, and has Dart’s dart command-line interface in its bin folder.
I try to run it using Android Studio (as a Dart CLI app), and also via command line: /opt/flutter/bin/dart lib/bin/example_script.dart. Unfortunately, I get an error:
Error:
lib/bin/example_script.dart:1:8: Error: Not found: 'dart:ui'
import 'dart:ui';
^
lib/bin/example_script.dart:4:17: Error: Method not found: 'Color'.
const color = Color(0xFF9585E5);
^^^^^
I have confirmed I have the latest Flutter, which provides Dart 2.18.4, which contains /opt/flutter/bin/cache/dart-sdk/lib/io/io.dart.
There are lots of seemingly-related answers, but relevant to Flutter apps. I'm writing a dart script.

After getting stuck on this for a while...
dart:ui is not a Dart core library, you won't find it in "Dart core libraries", so it is not available in dart packages. This was confusing to me because the name sounds like a core library. I was confusing it with dart:io 😂
As per dart:UI library docs:
Built-in types and core primitives for a Flutter application.
I confirmed that dart:ui is provided in /opt/flutter/bin/cache/pkg/sky_engine/lib/ui/ui.dart (Sky Engine being the old name for Flutter), not the Dart SDK (/opt/flutter/bin/cache/dart-sdk). So it seems like it's only available in Flutter.

Related

How can I add iOS support via dart ffi without adding flutter sdk as a dependency?

I am working on the dart_ping package and although it currently is tagged with iOS support, it does not actually support iOS. This is due to the way pub.dev detects supported platforms. I have filed an issue on dart-sdk repo.
Nonetheless, I would like to add iOS support while still preserving support for dart native. Right now, dart_ping works by calling the host OS's ping binary. The ping binary is available on Windows, macOS, Linux, and Android but not on iOS.
Current methods of performing a ping on iOS use native Objective-C code and call them via either method channels or ffi. Since method channels are a flutter feature rather than a dart feature, using them would require dart_ping to depend on the flutter sdk, which would prevent it from being used in a dart native application (perhaps server side).
Ffi on the other hand, is a dart native feature and is capable of doing what I want. However, I cannot seem to find a way to include the iOS framework/library/binary for ping in my dart_ping package in a way that instructs a flutter application to include it and link it to the iOS target/runner.
Is there any way to keep dart native support in my package while also supporting the edge case of iOS needing an extra non-dart framework? Even federated plugins depend on the flutter sdk. It seems to me that we need a pure dart equivalent to the federated plugin system.
TLDR: How do I add a pre-compiled iOS framework or binary to a dart package without depending on the flutter sdk and call it only when the package is used on iOS ?
For your specific case of adding iOS support only for Flutter users of your package but keeping the package as pure Dart without a dependency on the Flutter SDK should be possible by having a Flutter plugin package that depends on your Dart package.
In the pure Dart package you can have FFI calls to a native iOS library, but its actually the Flutter plugin package that supplies the native iOS library per the Flutter documentation for using FFI.
You would of course need to make it very clear in the documentation for your pure Dart package that for iOS, it should only be used via the "wrapper" plugin package.
Unfortunately this is a rather convoluted way to do it, but I don't know of a better way to achieve this as of now.
There is further discussion on this topic in the comments in this issue, which as of today is still open.

Extend Dart analyzer features

I'm currently building a framework on Dart and would like to throw static errors and warnings for the user on the 'Problems' tab on VS Code.
If not possible, throw warnings based on annotations would be very helpful, like the meta package.
I more or so understand the dart analyzer package but I can't see how could I integrate it (if possible at all) with the dart-code extension for VS Code.
Will I have to implement a whole extension for VS Code?

Loading native C library from Objective-C in Flutter plugin

I have a native C library (.so) build in versions for android and iOS. I want them to be used in a flutter app I am building.
For android I use System.loadLibrary() in Java to load the file (wrapped in JNI) and it is included via the Gradle build system/CMake.
But how do I get the same for iOS in objective C ?
Seems like I can use dlopen() on the library and then dlsym() every single function to get their symbol into the app at runtime. Seems pretty dump but I guess it could work (when I at some point have convinced the buildsystem to include the lib into the actual package it builds.

Why Flutter copies the src of standard libraries to sky_engine?

Since dart:ffi is available from Dart 2.2.0-dev.2.0, I've been trying to use that library. The sample app works fine for me and I also tried to use it for my Flutter app but I couldn't call it with import "dart:ffi", although Flutter on my machine was HEAD of master that used Dart 2.3.0-dev.0.0.
I checked what happened, then I found that sky_engine didn't contain ffi.dart while it contained other libraries (e.g. "dart:core"). Also I noticed that the source files of those libraries were copied from $FLUTTER_ROOT/bin/cache/dart-sdk/lib to $FLUTTER_ROOT/bin/cache/pkg/sky_engine using BUILD.gn or _embedder.yaml and that seemed to be why I couldn't use the dart:ffi in my Flutter app.
However, in the first place, why does Flutter need sky_engine, which is "the interface between Dart and the Flutter Engine"? Why not calling them directly without this glue code?
Flutter has a good documentation for FFI to be able to call native C APIs. Adding import 'dart:ffi' as ffi; on the Flutter app works without issues as of my testing with Flutter 2.5. If you're able to provide a minimal repro of your issue, this will help folks to understand the question better.
As for the question on why sky_engine is used by Flutter, that's just simply because it's the "flutter_engine" - similar to what has been already mentioned in the comments.

using import 'dart:html' in flutter - Do I need additional dependencies?

I came across a websocket example that I would like to use. However it uses import 'dart:html';. When I introduce that in my Flutter project seems like its not being picked up. Do I need to add additional dependencies to the pubspec.yaml?
dart:html can't be used in Flutter.
It is for browser applications only.
dart:html also only comes with the regular Dart SDK, not with the Dart SDK shipped with Flutter.
I know this is an old question but let me drop this answer here.
I've searched for a web crawler/scraper for Flutter for a while now. I've tried to use FlutterWebview and also the HTML package but no way. Recently I've found a new package for this.
The advantage of this package is that it is really cross platform as explained:
Cross-platform dart:html that works in the browser, Dart VM, and Flutter.
Typical use cases are:
Cross-platform application development (e.g. Flutter mobile and web versions).
Web crawling and scraping
you can use universal_html for any scraping/crawling purpose
Since the merge of Flutter-web into the main Flutter repository, it's no longer possible to directly add imports for web libraries (e.g. dart:html) in a Flutter project on the main channel when targeting Web, Android and iOS.
Use the universal html package which provides extensive support for multiple platforms and web libraries.
From the root level of your project, command
1.
flutter pub add universal_html
2. import 'package:universal_html/html.dart' as html
This package isn't required to run some web files (e.g. dart:js). In my case, I just had to remove the import 'dart:js' import statement.

Resources