pubspec_parse icon indicating copy to clipboard operation
pubspec_parse copied to clipboard

Provide an example?

Open lsgd opened this issue 2 years ago • 0 comments

I'm having a hard time to parse the pubspec.yaml, mainly I can't access the content of my pubspec.yaml

If I try to simply read it with File(), I get the following error:

final pubspec = await File('pubspec.yaml').readAsString();

Exception has occurred.
PathNotFoundException (PathNotFoundException: Cannot open file, path = 'pubspec.yaml' (OS Error: No such file or directory, errno = 2))

If I use rootBundle, I get another error:

final pubspec = rootBundle.loadString(
      "packages/<my_package_name>/pubspec.yaml",
    );

FlutterError (Unable to load asset: "packages/<my_package_name>/pubspec.yaml".
The asset does not exist or has empty data.)

I replaced <my_package_name> with the name specified in pubspec.yaml, as well as with the package attribute value <manifest [...] package="tld.mydomain.myapp"> from the AndroidManifest.xml. Both times it failed.


My code looks the following:

import 'package:pubspec_parse/pubspec_parse.dart';

[...]

  Future<String> getVersion() async {
    final pubspec = await File('pubspec.yaml').readAsString();
    final parsed = Pubspec.parse(pubspec);
    return parsed.version == null ? '?' : parsed.version!.canonicalizedVersion;
  }

and my file structure looks like this

./pubspec.yaml
./lib/src/screens/imprint.dart     // <-- contains function getVersion()

I haven't modified anything else, like I didn't add "pubspec.yaml" to assets in pubspec.yaml or similar. But even if I add "pubspec.yaml", the code fails with the PathNotFoundException error. Do I miss something?

It would be really helpful if the README would contain a working example to showcase how to use this module. Thanks a lot.

lsgd avatar Oct 14 '23 21:10 lsgd