flutter_sqlite_orm 0.1.1
flutter_sqlite_orm: ^0.1.1 copied to clipboard
A Flutter SQLite ORM package similar to Odoo's ORM, providing an elegant abstraction over SQLite with support for complex relationship fields.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_sqlite_orm/flutter_sqlite_orm.dart';
import 'models/customer.dart';
import 'models/order.dart';
import 'models/product.dart';
import 'models/order_line.dart';
import 'screens/customer_screen.dart';
import 'screens/product_screen.dart';
import 'screens/order_screen.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Register models with the ORM
FlutterSqliteOrm.registerModel(Customer());
FlutterSqliteOrm.registerModel(Product());
FlutterSqliteOrm.registerModel(Order());
FlutterSqliteOrm.registerModel(OrderLine());
// Initialize the ORM and create tables automatically
await FlutterSqliteOrm.initialize(databaseName: 'example.db', createTables: true);
// Insert sample data if needed
await _insertSampleData();
runApp(const MyApp());
}
Future<void> _insertSampleData() async {
// Check if we already have data
final customerCount = await BaseModel.searchCount<Customer>(Customer, []);
if (customerCount > 0) return;
// Create customers
final customer1 = Customer();
await customer1.create({
'name': 'John Doe',
'email': 'john@example.com',
'phone': '555-1234',
});
final customer2 = Customer();
await customer2.create({
'name': 'Jane Smith',
'email': 'jane@example.com',
'phone': '555-5678',
});
// Create products
final product1 = Product();
await product1.create({
'name': 'Laptop',
'description': 'High-performance laptop',
'price': 999.99,
});
final product2 = Product();
await product2.create({
'name': 'Smartphone',
'description': 'Latest smartphone model',
'price': 699.99,
});
final product3 = Product();
await product3.create({
'name': 'Headphones',
'description': 'Noise-cancelling headphones',
'price': 149.99,
});
// Create orders
final order1 = Order();
await order1.create({
'name': 'ORD001',
'date': DateTime.now(),
'customer_id': customer1.id.value,
});
// Create order lines
final orderLine1 = OrderLine();
await orderLine1.create({
'order_id': order1.id.value,
'product_id': product1.id.value,
'quantity': 1.0,
'price': product1.price.value,
});
final orderLine2 = OrderLine();
await orderLine2.create({
'order_id': order1.id.value,
'product_id': product3.id.value,
'quantity': 2.0,
'price': product3.price.value,
});
// Create another order
final order2 = Order();
await order2.create({
'name': 'ORD002',
'date': DateTime.now(),
'customer_id': customer2.id.value,
});
final orderLine3 = OrderLine();
await orderLine3.create({
'order_id': order2.id.value,
'product_id': product2.id.value,
'quantity': 1.0,
'price': product2.price.value,
});
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter SQLite ORM Example',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const HomePage(),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter SQLite ORM Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const CustomerScreen()),
);
},
child: const Text('Customers'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const ProductScreen()),
);
},
child: const Text('Products'),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const OrderScreen()),
);
},
child: const Text('Orders'),
),
],
),
),
);
}
}