6 Best Flutter ORM and Database Packages

Flutter is a powerful mobile development platform that enables you to quickly build high-quality apps for iOS and Android. It offers a lot of flexibility and options, including support for ORM (Object-Relational Mapping) and database packages. In this article, we’ll take a look at 6 of the best ORM and database packages.

Recommended ORM and Database Packages

Following are the best ORM frameworks for Flutter.

sqfentity

QFEntity is an ORM framework for CRUD operations and JSON data synchronization to your local SQLite database from the web.

//Create table
const tableStudent = SqfEntityTable(
  tableName: 'category',
  primaryKeyName: 'id',
  primaryKeyType: PrimaryKeyType.integer_auto_incremental,
  useSoftDeleting: true,
  modelName: null,
  fields: [
    SqfEntityField('name', DbType.text),
    SqfEntityField('isActive', DbType.bool, defaultValue: true),
  ]
);
//query data
final students = await Student().select().toList();
Student student = await Student().getById(10);
//save data 
final lastID = await Student(name: "John", isActive: true).save();

objectbox 

You’ve just created the next big hit app, but you quickly realize that you need a fast, reliable database to store all of your users’ data. With so many options out there, it can be hard to decide which database is right for your needs. Do you go with a traditional SQL database? Or maybe a NoSQL solution? What about ACID compliance?

objectbox is a good choice. This super-fast Flutter database offers high performance and ACID compliance, making it the perfect choice for any real-time application. Plus, the cross-platform support means that you can use objectbox on Android, iOS, macOS, Linux, and Windows.

@Entity()
class Person {
  int id;

  String firstName;
  String lastName;

  Person({this.id = 0, required this.firstName, required this.lastName});
}

final store = await openStore(); 
final box = store.box<Person>();

var person = Person(firstName: 'Joe', lastName: 'Green');

final id = box.put(person);  // Create

person = box.get(id)!;       // Read

person.lastName = "Black";
box.put(person);             // Update

box.remove(person.id);       // Delete

// find all people whose name start with letter 'J'
final query = box.query(Person_.firstName.startsWith('J')).build();
final people = query.find();  // find() returns List<Person>

hive

Hive is a lightweight and blazing fast key-value database written in pure Dart. It’s cross-platform, so it works on mobile, desktop, and browser apps. And it has a simple, powerful, and intuitive API that makes it easy to use. Plus, Hive comes with strong encryption built-in so your data is always safe.

@HiveType(typeId: 0)
class Person extends HiveObject {

  @HiveField(0)
  String name;

  @HiveField(1)
  int age;
}

jaguar_orm

Jaguar ORM is a source-generated ORM with relations , preloading, cascading, polymorphic relations, composite primary keys, and composite foreign keys.

class Account {
  @PrimaryKey()
  String id;

  String name;

  static const String tableName = '_account';

  String toString() => "Account($id, $name)";
}
@GenBean()
class AccountBean extends Bean<Account> with _AccountBean {
  AccountBean(Adapter adapter) : super(adapter);
}

postgrest

This is a PostgREST client for Dart. The aim is to make an “ORM-like” restful interface.

var url = 'https://example.com/postgrest/endpoint';
var client = PostgrestClient(url);

//read data
var response = await client.from('accounts').select().execute();
//update data
var response = await client.from('accounts').update({ 'status': 'inactive' }).eq('username', 'john').execute();
//delete data
var response = await client.from('accounts').delete().eq('username', 'johnfake').execute();

sqlbrite

This is a reactive stream wrapper around sqflite for Flutter.

//connect to database
final Database db = await openDb();
final briteDb = BriteDatabase(db);

//entity
class Account {
  factory Account.fromJson(Map<String, dynamic> map) { ... }
  
  factory Account.default() { ... }

  Map<String, dynamic> toJson() { ... }
}

// query
final Stream<List<Entity>> listQuery$ = briteDb.createQuery(
  'account',
  where: 'name LIKE ?',
  whereArgs: [queryName],
).mapToList((row) => Account.fromJson(row));

Leave a Comment

Your email address will not be published.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close