oobium_datastore 1.1.1 oobium_datastore: ^1.1.1 copied to clipboard
Persistent DataStore for Dart applications.
import 'dart:math';
import 'package:example/main.schema.g.dart';
import 'package:flutter/material.dart';
import 'package:oobium_datastore/oobium_datastore.dart';
import 'package:path_provider/path_provider.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final dir = await getApplicationDocumentsDirectory();
final path = '${dir.path}/demo-data';
final ds = MainData(path: path);
await ds.reset();
runApp(MyApp(ds));
}
class MyApp extends StatelessWidget {
final MainData ds;
MyApp(this.ds);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Oobium Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(
title: 'Oobium DataStore Demo',
ds: ds,
),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
final MainData ds;
MyHomePage({Key? key,
required this.title,
required this.ds,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
late final AnimationController controller;
late final Animation<double> angleAnimation;
@override
void initState() {
super.initState();
controller = AnimationController(vsync: this, duration: Duration(seconds: 5));
angleAnimation = Tween<double>(begin: 0, end: 2*pi).animate(controller);
controller.addListener(() {
setState(() {});
});
controller.repeat();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Transform.rotate(
angle: angleAnimation.value,
child: Container(
height: 200,
width: 200,
color: theme.primaryColor,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: const EdgeInsets.all(8),
child: Text('Model Count: ${widget.ds.feature<PersistFeature>().modelCount}', style: TextStyle(color: Colors.white)),
),
Padding(
padding: const EdgeInsets.all(8),
child: Text('Record Count: ${widget.ds.feature<PersistFeature>().recordCount}', style: TextStyle(color: Colors.white)),
),
],
),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 100),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: Text('Add'),
onPressed: () {
final l = widget.ds.items.length;
List.generate(100, (i) => ItemSnap(id: i + l, name: 'item-1-$i')).forEach((item) => widget.ds.items.load(item));
},
),
Container(width: 16,),
ElevatedButton(
child: Text('Update'),
onPressed: widget.ds.items.isEmpty ? null : () {
List.generate(100, (i) => ItemSnap(id: i, name: 'update-${++updateTaps}-$i')).forEach((item) => widget.ds.items.load(item));
},
),
Container(width: 16,),
ElevatedButton(
child: Text('Remove'),
onPressed: widget.ds.isEmpty ? null : () {
widget.ds.items.take(100).toList().forEach((item) => item.remove());
},
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
child: Text('Reset'),
onPressed: () {
widget.ds.reset();
},
),
],
),
],
),
),
],
),
),
);
}
}
int updateTaps = 0;