Pergunta

I have a simple dart project, it has file structure like:

darttest  tree
.
├── packages
│   ├── browser -> /Users/freewind/.pub-cache/hosted/pub.dartlang.org/browser-0.5.20/lib
│   ├── meta -> /Users/freewind/.pub-cache/hosted/pub.dartlang.org/meta-0.5.20/lib
│   ├── start -> /Users/freewind/.pub-cache/hosted/pub.dartlang.org/start-0.0.8/lib
│   └── unittest -> /Users/freewind/.pub-cache/hosted/pub.dartlang.org/unittest-0.5.20/lib
├── pubspec.lock
├── pubspec.yaml
└── src
    └── server.dart

It uses the library start, and is very simple.

The pubspec.yaml:

name: darttest
dependencies:
  start: any

The src/server.dart:

import 'package:start/start.dart';

void main() {
  start(public: 'web', port: 3000).then((Server app) {
    app.get('/').listen((request) {
      request.response
        .header('Content-Type', 'text/html; charset=UTF-8')
        .send('Hello, dart');
    });
  });
}

When I on darttest dir and run:

➜  darttest  dart src/server.dart
Unable to open file:
/Users/freewind/dev/workspace/darttest/src/packages/start/start.dart
'file:///Users/freewind/dev/workspace/darttest/src/server.dart': Error: line 1 pos 1: 
library handler failed
import 'package:start/start.dart';
^

It tries to find the start from darttest/src/packages/start/start.dart, but there is no such file, actually, the start in inside darttest/packages.

Do I have to put the server.dart inside the root of project? Is there any rule for this? Or how to fix this error?

Foi útil?

Solução

EDIT
pub install was renamed to pub get (pub install is still available as an undocumented alias for pub get)
EDIT

pub install solves this issue by creating additional packages directories that link to the main packages directory at the root of your package. It assumes your package is laid out according to the package layout guide, and creates a linked packages directory in bin/, test/, and example/, as well as their subdirectories

Dart Editor

If you are using the Dart Editor it can automaticly create a directory link to the main package folder.

You need to choose "Pub Install" from the context menu on the pubspec.yaml file in the Files view.

Command Line

If you want to create the directory from the command line you can use the pub install command. You need to run it in the directory of your pubspec.yaml file.

PS: Take a look at the Package layout conventions for Dart packages. They recommend to use the bin folder for standalone Dart applications.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top