This tutorial shows how to read and write properties files in Dart using the shared_preferences
package.
shared_preferences
is a dart and flutter package, used to parse and store key and value pairs, In Real time projects, you can use storage configuration and settings. It supports basic scalar types such as numbers, strings, and arrays of strings or numbers.
To use the shared_preferences
package in the project, add dependency in pubspec.yaml
name: example
description: example to read
version: 0.0.1
environment:
sdk: '>=2.0.8 <3.0.0'
dependencies:
shared_preferences: ^2.0.8
Next, Install dependency using pub get
command in the terminal.
In Dart, You can use the below
dart pub get
In the Flutter project use below
flutter pub get
This example explains reading properties files and store key and value pairs in shared
It also writes the key and value pairs object to write the file.
Let’s define config.properties
host= localhost
port= 3306
dbname= employes
username= root
password= root
How to read properties in Dart
Following are steps to parse properties in Ruby.
import
shared_preferences/shared_preferences.dart
package using import keyword.Create an instance using
SharedPreferences.getInstance()
It provides below methods to read the values of a key
- getString() for string values
- getInt for numbers
- getBool() for boolean values
Next, Create a Map object with the given values
read-properties.dart:
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
Future<Map<String, dynamic>> readProperties() async {
SharedPreferences map = await SharedPreferences.getInstance();
String host = map.getString('host') ?? '';
int port = map.getInt('port') ?? 0;
String username = map.getString('username') ?? '';
String dbname = map.getString('dbname') ?? '';
String password = map.getString('password') ?? '';
Map<String, dynamic> result = {
'host': host,
'port': port,
'dbname': dbname,
'username': username,
'password': password,
};
return result;
}
// Example usage
void main() async {
Map<String, dynamic> properties = await readProperties();
print('Read Properties: $properties');
}
Running the ruby read-properties.dart in the terminal gives the following output
host=localhost
dbname= employee
port= 3306
username= root
password= root
Write Map object to properties file in Dart
- Create and initialize a dictionary of key and value pairs
- get SharedPreferences object
- Set the values using setString, setInt, setBool methods
write-properties.dart:
Future<void> writeProperties(Map<String, dynamic> properties) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
// Replace with values
prefs.setString('host', properties['host']);
prefs.setInt('port', properties['port']);
prefs.setString('dbname', properties['dbname']);
prefs.setString('username', properties['username']);
prefs.setString('password', properties['password']);
}
// Example for writing properties
void main() async {
Map<String, dynamic> properties = {
'host': `localhost`,
'port': '3016',
'dbname': 'students',
'username': `root`,
'password': `root`,
};
await writeProperties(properties);
}
Running the code dart write-properties.dart
on the terminal creates properties and the output is
host=localhost
port=3016
dbname= students
username= root
password= root
sharedPreferences is used to write and read key and value pairs for configuration settings.