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.

; Created by Franc

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.

  ini: ^2.1.0

Here is a complete pubspec.yaml file.

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

  sdk: '>=2.10.0 <3.0.0'
  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.

; Created by Franc


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 async completed action then method, Convert the line string into Ini format using 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");
      .then((lines) => new Config.fromStrings(lines))
      .then((Config config) => {print(config)});


name = Franc
updateDate = 20-03-2020.
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 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.set("Author", "name", "john");

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


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

name = john
host = localhost
port = 4158
username = user
password = pwd

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

Methods Description output
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 false true
(config.get(“Author”, “name”) read the value for a get section and key Franc
config.hasOption(“Author”, “name”) checks key exists for a given section true
config.addSection(“Author”) Creates an given section NA
config.set(“Author”, “name”,“Franc”) Creates an configuration for a key and value for a given section NA
config.removeSection(“Author”) Removes section true
config.removeOption(“Author”, “name”) removes key and value for a given section true