model 0.1.3
model: ^0.1.3

Flutter Android iOS

Manage database easier. Use models to represent rows of database. Create database structure using object-oriented dart and leave all SQL strings hidden.

example/main.dart

import 'package:model/model.dart';

// Create models Author and Book by extending Model class.

class Author extends Model {
  static const nameCol = 'name';

  String name;

  Author({this.name});

  @override
  Map<String, dynamic> toMapForDB() => {nameCol: name};

  @override
  void constructFromDB(Map<String, dynamic> row) {
    name = row[nameCol] as String;
  }
}

class Book extends Model {
  static const titleCol = 'title';
  static const descriptionCol = 'description';
  static const priceCol = 'price';
  static const numberOfPagesCol = 'number_of_pages';
  static const authorIdCol = 'author_id';

  String title;
  String description;
  double price;
  int numberOfPages;
  int authorId;

  Book({
    this.title,
    this.description,
    this.price,
    this.numberOfPages,
    this.authorId,
  });

  @override
  Map<String, dynamic> toMapForDB() => {
        titleCol: title,
        descriptionCol: description,
        priceCol: price,
        numberOfPagesCol: numberOfPages,
        authorIdCol: authorId,
      };

  @override
  void constructFromDB(Map<String, dynamic> row) {
    title = row[titleCol] as String;
    description = row[descriptionCol] as String;
    price = row[priceCol] as double;
    numberOfPages = row[numberOfPagesCol] as int;
    authorId = row[authorIdCol] as int;
  }
}

// Extend DBProvider class to provide database.

class MyDBProvider extends DBProvider {
  @override
  DatabaseDescriber get describer => DatabaseDescriber(
        name: 'books',
        version: 1,
        tables: <Table>{
          Table(
            name: Book().tableName,
            columns: <Column>{
              Column(Book.titleCol, SQLiteType.text, <Constraint>{
                Unique(),
                NotNull(),
                Default(''),
              }),
              Column(Book.descriptionCol, SQLiteType.text),
              Column(Book.priceCol, SQLiteType.real),
              Column(Book.numberOfPagesCol, SQLiteType.integer),
              Column(Book.authorIdCol, SQLiteType.integer, <Constraint>{
                References(Author().tableName),
              })
            },
          ),
          Table(
            name: Author().tableName,
            columns: <Column>{
              Column(Author.nameCol, SQLiteType.text, <Constraint>{
                Unique(),
                NotNull(),
              })
            },
          ),
        },
      );
}

// Main method shows the use of the provider together with the models.

void main() async {
  // Open the database.
  final dbProvider = await MyDBProvider().open();

  // Create and save new author.
  final author = Author(name: 'foo');
  await author.save();

  final mergedAuthor = Author()
    ..id = author.id
    ..merge();
  print('Merged author: ${mergedAuthor.name}');
  print('Author: ${author.name}');
  print('Author are same: ${mergedAuthor.name == author.name}');

  // Create and save new book.
  final book = Book(
    title: 'bar',
    description: 'foo bar',
    price: 10.0,
    authorId: author.id,
  );
  await book.save();

  // Print tables.
  dbProvider.printTables();

  // Remove book from the database.
  book.delete();

  // Book should no longer exist.
  dbProvider.printTables();
}
0
likes
100
pub points
59%
popularity

Manage database easier. Use models to represent rows of database. Create database structure using object-oriented dart and leave all SQL strings hidden.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

strazanec.roman@gmail.com

License

BSD (LICENSE)

Dependencies

flutter, path, sqflite

More

Packages that depend on model