{

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples


This tutorials explains about how to read pubspec.yaml file in example.

pubspec.yaml file contains following things

name: dartapp
description: >-
      dart example application.
version: 1.0.0

environment:
  sdk: '>=2.10.0 <3.0.0'
dependencies:
  ini: ^2.1.0
  jiffy: ^5.0.0
  quiver: 3.0.1+1
  yaml: ^3.1.0
  yaml_writer: 1.0.1
  image: any
  mime: any
  path: any
  package_info: any 
dev_dependencies:
    intl: any

How to read pubspec.yaml file in Dart

This tutorials is about to read puspect.yaml file and print the content.

yaml package provides functions for reading yaml syntax files.

  • Create an File object with pubspec.yaml
  • read the file using readAsString, that returns Future<String> with given yaml content.
  • Convert yaml String into Map using loadYaml function.
  • Read the attributes from map using Map[key] syntax.

Here is an example program to read pubspec.yaml file

import 'package:yaml/yaml.dart';
import 'dart:io';

void main() {
  File file = new File("pubspec.yaml");
  file.readAsString().then((String content) {
    Map yaml = loadYaml(content);
    print(yaml['name']);
    print(yaml['description']);
    print(yaml['version']);
    print(yaml['dependencies']);
    print(yaml['dev_dependencies']);
  });
}

Output:

dartapp
dart example application.
1.0.0
{ini: ^2.1.0, jiffy: ^5.0.0, quiver: 3.0.1+1, yaml: ^3.1.0, yaml_writer: 1.0.1, image: any, mime: any, path: any, package_info: any}
{intl: any}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example

Dart| Flutter How to: Create a private variable with example