objectdb_flutter 0.0.4

objectdb_flutter #

Reactive ObjectDB helper.

CAUTION This plugin is still in development. Use at your own risk. If you notice any bugs you can create an issue on GitHub. You're also welcome to contribute using pull requests. Please open an issue before spending time on any pull request.

Subscribe to store inside widget to keep the view up-to-date.

See example for implementation and ObjectDB for general db API.

[0.0.1] - Initial release

  • Flutter ObjectDB (embedded document-oriented NoSQL database) -listener with reactive store

example/lib/main.dart

import 'dart:io';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart' as path;
import 'package:objectdb_flutter/objectdb_flutter.dart';

// declare db object
ObjectDB db;

void main() async {
  // get document directory using path_provider plugin
  Directory appDocDir = await path.getApplicationDocumentsDirectory();

  String dbFilePath = [appDocDir.path, 'user.db'].join('/');

  // delete old database file if exists
  File dbFile = File(dbFilePath);

  // check if database already exists
  var isNew = !await dbFile.exists();
  if (!isNew) dbFile.deleteSync();
  // initialize and open database
  db = ObjectDB(dbFilePath);
  db.open();

  // insert sample data
  db.insertMany([
    {
      'name': {'first': 'Alex', 'last': 'Boyle'},
      'message': 'abc',
      'active': true,
      'count': 0,
    },
    {
      'name': {'first': 'Maia', 'last': 'Herzog'},
      'message': 'def',
      'active': true,
      'count': 0,
    },
    {
      'name': {'first': 'Curtis', 'last': 'Smith'},
      'message': 'ghi',
      'active': true,
      'count': 0,
    },
    {
      'name': {'first': 'Jaquelin', 'last': 'Renner'},
      'message': 'jkl',
      'active': false,
      'count': 0,
    },
    {
      'name': {'first': 'Denis', 'last': 'Swift'},
      'message': 'mno',
      'active': true,
      'count': 0,
    },
    {
      'name': {'first': 'Anna', 'last': 'Metz'},
      'message': 'pqr',
      'active': true,
      'count': 0,
    },
    {
      'name': {'first': 'Malinda', 'last': 'Reynolds'},
      'message': 'stu',
      'active': false
    },
  ]);

  // simulate insert/update/delete
  int i = 0;
  new Timer.periodic(Duration(seconds: 2), (Timer t) {
    db.insert({
      'name': {'first': 'Malinda', 'last': 'Reynolds'},
      'message': 'hoho ' + i.toString(),
      'active': i % 2 == 0,
      'count': i++
    });

    if (i > 10) i = 0;
  });

  new Timer.periodic(Duration(seconds: 10), (Timer t) {
    db.update({
      Op.gte: {'count': 5}
    }, {
      Op.set: {'message': 'updated'}
    });
  });

  new Timer.periodic(Duration(seconds: 15), (Timer t) {
    db.remove({'message': 'updated'});
  });

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ObjectDB Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'ObjectDB Demo Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage>
    with StoreWatcherMixin<MyHomePage> {
  Store contacts;

  void initState() {
    super.initState();

    init();
  }

  void init() async {
    contacts = await listenToStore(db, {'active': true});
  }

  @override
  void dispose() async {
    await db.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: contacts == null // check if _contacts has been initialized
          ? Center(
              child: Text('loading...'),
            )
          : ListView(
              children:
                  contacts.map((contact) => contactItem(contact)).toList(),
            ),
    );
  }
}

// creates list tile with contact info
Widget contactItem(Map contact) {
  return ListTile(
    title: Text(contact['name']['first'] + ' ' + contact['name']['last']),
    subtitle: Text(contact['message']),
  );
}

Use this package as a library

1. Depend on it

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


dependencies:
  objectdb_flutter: ^0.0.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:objectdb_flutter/objectdb_flutter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
70
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
83
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.0.0 <3.0.0
flutter 0.0.0
objectdb ^1.0.5 1.0.7+1
Transitive dependencies
collection 1.14.11
deeply 1.0.1
execution_queue 1.0.2
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test

Admin