PubspecReader class

A utility class for reading and parsing dependencies from pubspec.yaml.

This class provides a simple interface to extract dependency information from a Flutter/Dart project's pubspec.yaml file. It works in conjunction with PubspecLoader to locate and load the configuration file.

Usage

import 'package:pubspec_checker/pubspec_checker.dart';

void main() {
  // Create a PubspecReader instance
  final reader = PubspecReader();

  // Read dependencies from pubspec.yaml
  final dependencies = reader.getDependencies();

  // Process the dependencies
  print('Found ${dependencies.length} dependencies:');
  dependencies.forEach((package, version) {
    print('  $package: $version');
  });
}

Output Example

// Sample dependencies map returned by getDependencies()
{
  'provider': '^6.0.0',
  'http': '^1.0.0',
  'flutter_bloc': '^8.0.0',
  'dio': '^5.0.0',
  'intl': '^0.18.0',
}

File Location

The reader automatically looks for pubspec.yaml in:

  1. The current working directory
  2. Parent directories (up to 3 levels up)
  3. The standard Flutter project location

Notes

  • Returns an empty map if no dependencies are found
  • Handles both simple version strings and complex version constraints
  • Works with Dart projects as well as Flutter projects
  • Throws exceptions if pubspec.yaml cannot be found or parsed

Integration with PlatformChecker

This class is typically used in conjunction with PlatformChecker:

final reader = PubspecReader();
final dependencies = reader.getDependencies();

final checker = PlatformChecker([
  PackagePlatform.android,
  PackagePlatform.ios,
]);

final compatibility = await checker.checkDependenciesCompatibility(
  dependencies,
);

Constructors

PubspecReader()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

getDependencies() Map<String, dynamic>
Reads and parses the dependencies section from the pubspec.yaml file.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited