Ikhwan Ikhwan - 1 month ago 18
Dart Question

How do I import a package in a simple Dart script (no pubspec.yaml)?

Let's say I have a simple script such as this:

// File main.dart
import 'package:uuid/uuid.dart';

main() {
print(new Uuid().v1());
}


And I run it from command line via
dart main.dart
.

How do I make the
import
work? The error I got was:

Could not import "package:uuid/uuid.dart" from "file:///C:/folder/main.dart":
Could not resolve a package location for base at file:///C:/folder/main.dart


I already did a
pub global activate uuid
.

Answer

pubspec.yaml and pub get (or pub upgrade) are used to make a package available to your project at all.
Only after a package is available libraries from this package can be imported using a package URI.

pub global activate is to make packages available that contain executables to be executed from the command line.

See also https://www.dartlang.org/tools/pub/get-started

Comments