flutter_pom 0.1.6

  • Readme
  • Changelog
  • Example
  • Installing
  • new67

flutter_pom #

Flutter Persistent Object Mapper based on sqflite provides model based SQLite access including automatic database creation

Installing #

Add dependency #

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

dependencies:
  flutter_pom: ^0.0.3

Install the plugin #

Now you can install the package (if you got it from pub.dev) via console.

$ flutter pub get

Import the plugin #

Finally you have to import the plugin into your Dart source

import 'package:flutter_pom/flutter_pom.dart';

Example #

To kickstart with the newly installed library you will have to create the necessary model and database classes.

Create the Table-model #

The table model represents the configuration of your table. This includes all columns and the table name. In order to work this needs to extend from Table (flutter_pom).

class SampleTable extends Table {
  // The constructor has to call 'super' with the name of the table
  SampleTable() : super("sample_table");
  
  // These are the fields that we define for the table.
  // Each field corresponds to a table column
  
  final IdField id = IdField("id").autoIncrement();
  final StringField str = StringField("str");
  
  // You have to override the method 'getInstance' for the
  // deserializer to get a new instance of your type as dart
  // does not support reflection well by now.
  
  @override
  Table getInstance() {
    return SampleTable();
  }
  
  // initializeFields provides the TableBuilder in the background
  // with all defined fields. As dart does not support reflection 
  // this is our way to go.
  
  @override
  List<Field> initializeFields() {
    return [
      id,
      str
    ];
  }
}

Create the Database model #

Next you have to create a database model. The model needs to be inherited from Database (flutter_pom). The database model contains all tables that you want to access inside the specified database.

Note: There can be more than one database model inside your app

class SampleDb extends Database {
  // The constructor has to call 'super' with the database name
  SampleDb() : super("sample.db");
  
  // initializeDatabase provides the DatabaseBuilder in the background
  // with all containing databases. As dart does not support reflection
  // this is our way to go.
  
  @override
  Map<Type, Table> initializeDatabase() {
    return <Type, Table>{
      SampleTable: SampleTable()
    };
  }
}

Use the database in your App Logic #

Now its time to make use of the newly created database and tables.

void Do() async {
  
    // initialize the database
    var db = SampleDb();
    
    // open() the connection to the database. 
    // This method has to be called once before accessing the database
    await db.open();
    
    // Get the automatically created context of the table 'SampleTable'
    var context = db.of<SampleTable>();
    
    // Create a new SampleTable item (think of it as a row)
    var sampleItem = SampleTable();
    
    // Access the str field
    sampleItem.str.value = "String value";
    
    // Put the item into the database
    await context.put(sampleItem);
    
    // Get all items from the table
    // If you do not provide any arguments to 'getRange()' it will return
    // all items from the selected table context
    var items = await context.getRange();
    
    // Get all items with filter
    var itemsFilter = await context.getRange(where: "str = 'String value'");
    
    // Order the items 
    var itemsOrder = await context.getRange(orderBy: 'str DESC');
    
    // Delete the item
    await context.delete(sampleItem);
    
    // Update the item. Only changed values will be updated.
    await context.update(sampleItem);
}

[0.1.6] - 17.01.2020

  • Removed migrations for now

[0.1.5] - 17.01.2020

  • Bugfix release

[0.1.4] - 17.01.2020

  • Bugfix release

[0.1.3] - 17.01.2020

  • Bugfix release

[0.1.2] - 17.01.2020

  • Bugfix release

[0.1.1] - 17.01.2020

  • Bugfix release

[0.1.0] - 17.01.2020

  • Added automatic migrations and overridable migrations

[0.0.9] - 16.01.2020

  • Fixed update statement to include primary keys and disable auto-increment fields

[0.0.8] - 16.01.2020

  • Added more reliability and flexibility for value to field mapping of various types
  • Implemented more unit tests for better coverage

[0.0.7] - 16.01.2020

  • Removed unnecessary constraints disabling primary-keys for several fields
  • Changed parameter signature of "fromSql" to dynamic

[0.0.6] - 16.01.2020

  • Added specific errors
  • Added Unit-Testing
  • Fixed bugs with handling notNull constraints
  • Fixed bugs with handling autoIncrement constraints
  • Fixed bugs with handling mapping errors

[0.0.5] - 16.01.2020

  • The export for SecureString was missing

[0.0.4] - 16.01.2020

  • Added SecureStringField to provide a hashed database field for storing passwords etc.
  • Changed field signature of StringField from VARCHAR(255) to TEXT

[0.0.3] - 16.01.2020

  • New MIT License agreement was added to the root package
  • Fixed install instructions to make use of pub.dev instead of using a local path
  • Changed Homepage to a secure URI

[0.0.2] - 16.01.2020

  • Started satisfying all required publishing specs

[0.0.1] - 16.01.2020

  • Initial Release

example/README.md

Example #

To kickstart with the newly installed library you will have to create the necessary model and database classes.

Create the Table-model #

The table model represents the configuration of your table. This includes all columns and the table name. In order to work this needs to extend from Table (flutter_pom).

class SampleTable extends Table {
  // The constructor has to call 'super' with the name of the table
  SampleTable() : super("sample_table");
  
  // These are the fields that we define for the table.
  // Each field corresponds to a table column
  
  final IdField id = IdField("id").autoIncrement();
  final StringField str = StringField("str");
  
  // You have to override the method 'getInstance' for the
  // deserializer to get a new instance of your type as dart
  // does not support reflection well by now.
  
  @override
  Table getInstance() {
    return SampleTable();
  }
  
  // initializeFields provides the TableBuilder in the background
  // with all defined fields. As dart does not support reflection 
  // this is our way to go.
  
  @override
  List<Field> initializeFields() {
    return [
      id,
      str
    ];
  }
}

Create the Database model #

Next you have to create a database model. The model needs to be inherited from Database (flutter_pom). The database model contains all tables that you want to access inside the specified database.

Note: There can be more than one database model inside your app

class SampleDb extends Database {
  // The constructor has to call 'super' with the database name
  SampleDb() : super("sample.db");
  
  // initializeDatabase provides the DatabaseBuilder in the background
  // with all containing databases. As dart does not support reflection
  // this is our way to go.
  
  @override
  Map<Type, Table> initializeDatabase() {
    return <Type, Table>{
      SampleTable: SampleTable()
    };
  }
}

Use the database in your App Logic #

Now its time to make use of the newly created database and tables.

void Do() async {
  
    // initialize the database
    var db = SampleDb();
    
    // open() the connection to the database. 
    // This method has to be called once before accessing the database
    await db.open();
    
    // Get the automatically created context of the table 'SampleTable'
    var context = db.of<SampleTable>();
    
    // Create a new SampleTable item (think of it as a row)
    var sampleItem = SampleTable();
    
    // Access the str field
    sampleItem.str.value = "String value";
    
    // Put the item into the database
    await context.put(sampleItem);
    
    // Get all items from the table
    // If you do not provide any arguments to 'getRange()' it will return
    // all items from the selected table context
    var items = await context.getRange();
    
    // Get all items with filter
    var itemsFilter = await context.getRange(where: "str = 'String value'");
    
    // Order the items 
    var itemsOrder = await context.getRange(orderBy: 'str DESC');
    
    // Delete the item
    await context.delete(sampleItem);
    
    // Update the item. Only changed values will be updated.
    await context.update(sampleItem);
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_pom: ^0.1.6

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:flutter_pom/flutter_pom.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
35
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
67
Learn more about scoring.

We analyzed this package on Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Health suggestions

Fix lib/model/database.dart. (-1 points)

Analysis of lib/model/database.dart reported 2 hints:

line 4 col 8: Unused import: 'package:flutter_pom/errors/table_configuration_error.dart'.

line 36 col 16: The declaration '_doMigrations' isn't referenced.

Fix lib/model/fields/object_field.dart. (-0.50 points)

Analysis of lib/model/fields/object_field.dart reported 1 hint:

line 1 col 8: Unused import: 'dart:convert'.

Fix lib/model/fields/secure_string_field.dart. (-0.50 points)

Analysis of lib/model/fields/secure_string_field.dart reported 1 hint:

line 33 col 17: Override hashCode if overriding ==.

Fix additional 18 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/context/migration_context.dart (Run flutter format to format lib/context/migration_context.dart.)
  • lib/errors/duplicate_field_error.dart (Run flutter format to format lib/errors/duplicate_field_error.dart.)
  • lib/errors/field_constraint_error.dart (Run flutter format to format lib/errors/field_constraint_error.dart.)
  • lib/errors/missing_field_error.dart (Run flutter format to format lib/errors/missing_field_error.dart.)
  • lib/errors/missing_primary_key_error.dart (Run flutter format to format lib/errors/missing_primary_key_error.dart.)
  • lib/errors/multiple_primary_key_error.dart (Run flutter format to format lib/errors/multiple_primary_key_error.dart.)
  • lib/errors/table_configuration_error.dart (Run flutter format to format lib/errors/table_configuration_error.dart.)
  • lib/model/field.dart (Run flutter format to format lib/model/field.dart.)
  • lib/model/fields/bool_field.dart (Run flutter format to format lib/model/fields/bool_field.dart.)
  • lib/model/fields/datetime_field.dart (Run flutter format to format lib/model/fields/datetime_field.dart.)
  • lib/model/fields/double_field.dart (Run flutter format to format lib/model/fields/double_field.dart.)
  • lib/model/fields/id_field.dart (Run flutter format to format lib/model/fields/id_field.dart.)
  • lib/model/fields/integer_field.dart (Run flutter format to format lib/model/fields/integer_field.dart.)
  • lib/model/fields/key_field.dart (Run flutter format to format lib/model/fields/key_field.dart.)
  • lib/model/fields/string_field.dart (Run flutter format to format lib/model/fields/string_field.dart.)
  • lib/model/migration_info.dart (Run flutter format to format lib/model/migration_info.dart.)
  • lib/model/serializable.dart (Run flutter format to format lib/model/serializable.dart.)
  • lib/model/table.dart (Run flutter format to format lib/model/table.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
crypto ^2.1.3 2.1.4
flutter 0.0.0
intl ^0.16.0 0.16.1
path ^1.6.4 1.6.4
path_provider ^1.5.1 1.5.1
sqflite ^1.2.0 1.2.0
Transitive dependencies
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
meta 1.1.8
platform 2.2.1
sky_engine 0.0.99
synchronized 2.1.1
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
matcher any