PubSpec.load constructor

PubSpec.load({
  1. String? directory,
  2. String filename = 'pubspec.yaml',
  3. bool search = true,
})

Loads the pubspec.yaml file from the given directory or the current work directory if directory is not passed.

If the pubspec is not found, we search up the directory tree looking for it unless search is set to false.

If you pass filename then it can be loaded from a non-standard filename.

If you don't pass filename then we will attempt to load the pubspec from the file 'pubspec.yaml'.

If you don't provide a directory then we start the search from the current working directory.

Pubspec.load()
  ..dependencies
    .append(HostedDependency(name: 'onepub', url:'https://onepub.flutter-io.cn'))
  ..save();

Implementation

factory PubSpec.load(
    {String? directory,
    String filename = 'pubspec.yaml',
    bool search = true}) {
  final loadedFrom = _findPubSpecFile(
      directory ?? io.Directory.current.path, filename, search);

  final document = Document.loadFrom(loadedFrom);

  final pubspec = PubSpec._loadFromDocument(document)
    .._loadedFromDirectory = dirname(loadedFrom)
    .._loadedFromFilename = basename(loadedFrom);
  return pubspec;
}