EasyFlutterORM
EasyFlutterORM is a simple, easy-to-use Object-Relational Mapping (ORM) solution for Flutter that allows you to work with SQLite databases using a straightforward API. This package simplifies database operations such as creating tables, inserting, querying, updating, and deleting records.
Features
- Easy initialization of the SQLite database.
- Simple API for CRUD (Create, Read, Update, Delete) operations.
- Supports dynamic schema creation.
- Lightweight and easy to integrate into any Flutter project.
Installation
Add the following dependency to your pubspec.yaml
file:
dependencies:
easy_flutter_orm: ^1.0.0
Then, run flutter pub get
to install the package.
Usage
1. Initialize the ORM
Initialize the ORM in the main.dart
file before running your app:
import 'package:flutter/material.dart';
import 'package:easy_flutter_orm/easy_flutter_orm.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized(); // Ensures Flutter is initialized before any async operation
await EasyFlutterORM.init('my_database.db');
runApp(MyApp());
}
2. Create a Table
You can create a table with the specified schema in your database:
await EasyFlutterORM().createTable(
'users',
{
'id': 'INTEGER PRIMARY KEY AUTOINCREMENT',
'name': 'TEXT',
'email': 'TEXT',
'phone': 'INTEGER',
},
);
3. Insert Data
Insert a new record into the table:
await EasyFlutterORM().insert(
'users',
{
'name': 'John Doe',
'email': 'john.doe@example.com',
'phone': '03153929992',
},
);
4. Query Data
Query records from the table:
List<Map<String, dynamic>> users = await EasyFlutterORM().query('users');
5. Update Data
Update an existing record:
await EasyFlutterORM().update(
'users',
{
'name': 'Jane Doe',
'email': 'jane.doe@example.com',
'phone': '933993933'
},
'id = ?',
[1],
);
6. Delete Data
Delete a record from the table:
await EasyFlutterORM().delete(
'users',
'id = ?',
[1],
);
7. Full Example
Here is a complete example of how to use EasyFlutterORM in a Flutter app:
import 'package:flutter/material.dart';
import 'package:easy_flutter_orm/easy_flutter_orm.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyFlutterORM.init('my_database.db');
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'EasyFlutterORM Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Map<String, dynamic>> _users = [];
@override
void initState() {
super.initState();
_initializeDatabase();
}
void _initializeDatabase() async {
await EasyFlutterORM().createTable(
'users',
{
'id': 'INTEGER PRIMARY KEY AUTOINCREMENT',
'name': 'TEXT',
'email': 'TEXT',
'phone': 'INTEGER',
},
);
await EasyFlutterORM().insert(
'users',
{
'name': 'John Doe',
'email': 'john.doe@example.com',
'phone': '03153929992',
},
);
List<Map<String, dynamic>> users = await EasyFlutterORM().query('users');
setState(() {
_users = users;
});
}
void _updateUser() async {
await EasyFlutterORM().update(
'users',
{
'name': 'Jane Doe',
'email': 'jane.doe@example.com',
'phone': '933993933'
},
'id = ?',
[1],
);
List<Map<String, dynamic>> users = await EasyFlutterORM().query('users');
setState(() {
_users = users;
});
}
void _deleteUser() async {
await EasyFlutterORM().delete(
'users',
'id = ?',
[1],
);
List<Map<String, dynamic>> users = await EasyFlutterORM().query('users');
setState(() {
_users = users;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('EasyFlutterORM Example'),
),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: _users.length,
itemBuilder: (context, index) {
return ListTile(
title: Text('${_users[index]['name']} ${_users[index]['phone']}'),
subtitle: Text(_users[index]['email']),
);
},
),
),
ElevatedButton(
onPressed: _updateUser,
child: Text('Update User'),
),
ElevatedButton(
onPressed: _deleteUser,
child: Text('Delete User'),
),
],
),
);
}
}
License
This package is licensed under the MIT License. See the LICENSE
file for more information.
Contributions
Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
This README.md
file provides a comprehensive guide on how to use your EasyFlutterORM
package, from initialization to performing basic CRUD operations.