Access to pubspec.yaml attributes (version) from Dart app

FOR FLUTTER ONLY

Please use this new package package_info_plus from flutter community.

import 'package:package_info_plus/package_info_plus.dart';

PackageInfo packageInfo = await PackageInfo.fromPlatform();

String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;

BELOW SOLUTION IS DEPRICATED.

I know the OP wants to read YAML but for flutter dev's you guys can read the version and other info of the application using package_info.

This is the sample to fetch details from Android/iOS application.

import 'package:package_info/package_info.dart';

PackageInfo packageInfo = await PackageInfo.fromPlatform();

String appName = packageInfo.appName;
String packageName = packageInfo.packageName;
String version = packageInfo.version;
String buildNumber = packageInfo.buildNumber;

you can install the "dart_config" package and use this code to parse a pubspec.yaml file:

import 'package:dart_config/default_server.dart';
import 'dart:async';

void main() {
  Future<Map> conf = loadConfig("../pubspec.yaml");
  conf.then((Map config) {
    print(config['name']);
    print(config['description']);
    print(config['version']);
    print(config['author']);
    print(config['homepage']);
    print(config['dependencies']);
  });
}

The output looks similar to this:

test_cli
A sample command-line application
0.0.1
Robert Hartung
URL
{dart_config: any}

EDIT

You can do it with the Yaml package itself:

*NOTE: this will not work on Flutter Web

import 'package:yaml/yaml.dart';
import 'dart:io'; // *** NOTE *** This will not work on Flutter Web

void main() {      
    File f = new File("../pubspec.yaml");
    f.readAsString().then((String text) {
      Map yaml = loadYaml(text);
      print(yaml['name']);
      print(yaml['description']);
      print(yaml['version']);
      print(yaml['author']);
      print(yaml['homepage']);
      print(yaml['dependencies']);
    });
}

Regards Robert

Tags:

Dart