This tutorial shows an example of how to read and write an ini file in Dart and flutter example using ini library

Dart read and write ini file

Dart language provides various third-party libraries to read and write ini files ini is a popular library to read and write ini files.

  • ini for reading and writing

This post is about an example of reading and writing an ini file using the ini.dart - dart library.

Sample ini file example

ini is a configuration file in Windows similar to the properties file. It contains key and value pairs with syntax rules.

Following is the ini example file config.ini which is used as a base for parsing, reading, and writing the data to it. Given the ini example file with database configuration details.

;config.ini
; Created by Franc
[Author]
name=Franc
updateDate=20-03-2020.

[database]
driverclass   = com.mysql.jdbc.Driver
dbName        = mydatabase
port          = 3306
username      = root
password      =

Adding ini dependency to pubspec.yaml

First, add the ini dependency to the pubspec.yaml file with the version in the Dart application.

dependencies:
  ini: ^2.1.0

Here is a complete pubspec.yaml file.

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

environment:
  sdk: '>=2.10.0 <3.0.0'
dependencies:
  ini: ^2.1.0

Next, Run dart pub get to install dependencies in the command line.

A:\work\dart>dart pub get
Resolving dependencies...
+ ini 2.1.0
Downloading ini 2.1.0...
Changed 1 dependency

Sample INI data example file

Following is the ini example file which is used as a base for parse, reading, and writing the data to it.

;config.ini
; Created by Franc
[Author]
name=Franc
updateDate=20-03-2020.

[database]
server=192.168.10.o
port=43
username="root"

Read and parse INI file in dart

The below example parses the ini file in Dart using the ini library. It reads the ini file as a string and prints the ini file content.

  • First, import the ini library into the dart code file
  • Next, create a file object with an ini file.
  • read the line by line from a file using readAsLines(), which returns async Future<List<String>> type.
  • using the async completed action then method, Convert the line string into Ini format using the Config.fromStrings() method.
  • Finally, print the config object that results in the ini file content.
import "package:ini/ini.dart";
import "dart:io";

void main() {
  File file = new File("config.ini");
  file
      .readAsLines()
      .then((lines) => new Config.fromStrings(lines))
      .then((Config config) => {print(config)});
}

Output:

[Author]
name = Franc
updateDate = 20-03-2020.
[database]
server = 192.168.10.o
port = 43
username = "root"

How to write an ini file in Dart?

This example of creating an ini file and writing a configuration to this file.

  • Created a file that creates a file in the file system in the current directory.
  • Create a Config object.
  • add the section using config.addSection() method
  • add the configuration to the section using config.set() method
import "package:ini/ini.dart";
import "dart:io";

void main() {
  File file = new File("output.ini");
  Config config = new Config();
  config.addSection("Author");

  config.set("Author", "name", "john");
  config.addSection("Database");

  config.set("Database", "host", "localhost");
  config.set("Database", "port", "4158");
  config.set("Database", "username", "user");
  config.set("Database", "password", "pwd");
  config.removeOption("Database", "ssl");

  file.writeAsString(config.toString());
}

Output: output.ini file created with the below settings.


[Author]
name = john
[Database]
host = localhost
port = 4158
username = user
password = pwd

There are other methods from the Config class from the ini library.

MethodsDescriptionoutput
sections()Prints list of sections(Author, database)
config.options(“Author”)Prints list of keys for an Section(name, updateDate)
config.items(“Author”)Prints list of key and values for an Section[[name, Franc], [updateDate, 20-03-2020.]]
config.hasSection(“Author”)Return true, if it is a section, else falsetrue
(config.get(“Author”, “name”)read the value for a get section and keyFranc
config.hasOption(“Author”, “name”)checks key exists for a given sectiontrue
config.addSection(“Author”)Creates an given sectionNA
config.set(“Author”, “name”,“Franc”)Creates a configuration for a key and value for a given sectionNA
config.removeSection(“Author”)Removes sectiontrue
config.removeOption(“Author”, “name”)removes key and value for a given sectiontrue

The same code works in Flutter with these packages.