local_database 1.1.0+1

A local database using dart:io file system components

Getting started #

In the pubspec.yaml of your flutter project, add the following dependency:

dependencies:
  ...
  local_database: ^1.1.0+1

In your library add the following import:

import "package:local_database/local_database.dart";

For help getting started with Flutter, view the online documentation.

Using #

This plugin comes with three main operations, get, put, and remove.

To use the database, simply instantiate it with the constructor Database database = new Database(/*File System Path to database*/);

Next, call the built in methods for database.

To put into the database, use the []= operator (note that only JSON encodable types can be used). The parameter of this operator is the path in the database to put to delimited by forward slashes ("/").

database["dir1"] = {
	"b":[1,2,3]
};
database["dir1/b/0"] = 100;

To read from the database, use the [] operator. The parameter of this operator is the path in the database to read from delimited by forward slashes ("/").

print(await database["dir1"]);
print(await database["dir1/b/0"]);

To remove from the database, use the .remove(String path) method. The parameter of this operator is the path in the database to put to delimited by forward slashes ("/").

database.remove("dir1");

The following is an example use of the package:

Database database = new Database(Directory.current.path+"/data");
Map<String,dynamic> userData;
if((await database["userData"])==null){
	String userId = "";
	Random r = new Random();
	for(int i = 0; i<8;i++){
		userId+=r.nextInt(10).toString();
	}
	database["userData"] = {
		"created": (new DateTime.now()).millisecondsSinceEpoch,
		"id":userId,
		"numLikes":0
	};
}
userData = await database["userData"];

1.1.0+1 #

  • Fixed typos

1.1.0 #

  • Reading is now asynchronous
  • Removing now returns void

1.0.1+4 #

  • Readme fixes

1.0.1+3 #

  • Fixed a typo in readme

1.0.1+2 #

  • Fixed changelog

1.0.1+1 #

  • Fixed a bug with getting from root when it is empty

1.0.1 #

  • Removed static method fromApplicationDocumentsDirectory

1.0.0+2 #

  • Readme fixes

1.0.0 #

  • Initial release

example/main.dart

import "dart:io";
import "package:local_database/local_database.dart";

void main() async {
  String home = "";
  Map<String, String> envVars = Platform.environment;
  if (Platform.isMacOS) {
    home = envVars["HOME"];
  } else if (Platform.isLinux) {
    home = envVars["HOME"];
  } else if (Platform.isWindows) {
    home = envVars["UserProfile"];
  } else {
    throw new Exception("Unknown platform");
  }
  Database database = new Database(home + "/data");
  //Put a complex map into path /dir1
  database["dir1"] = {
    "a": [1, 2, 3],
    "b": {"c": 5},
    "d": [
      1,
      2,
      {"e": 5}
    ]
  };
  //Put a single string into path /dir2/f
  database["dir2/f"] = "Data";
  //Read from /dir1
  print(await database["dir1"]);
  //Read from /dir1/a/0
  print(await database["dir1/a/0"]);
  //Read the entire database
  print(await database["/"]);
  //Read a nonexistent element
  print(await database["nonexistant"]);
  //Remove from the database
  database.remove("dir1");
  print(await database["dir1"]);
  new Directory(home + "/data")..deleteSync(recursive: true);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  local_database: ^1.1.0+1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:local_database/local_database.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
42
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

We analyzed this package on Sep 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
Dev dependencies
flutter_test