Created
July 5, 2019 08:55
-
-
Save simolus3/30f0b1fd6172384f3a77e6a03deea7bd to your computer and use it in GitHub Desktop.
Moor test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:moor_flutter/moor_flutter.dart'; | |
part 'database.g.dart'; | |
class Rooms extends Table { | |
IntColumn get id => integer().autoIncrement()(); | |
TextColumn get desc => text()(); | |
} | |
@UseMoor(tables: [Rooms]) | |
class MyDatabase extends _$MyDatabase { | |
MyDatabase() | |
: super(FlutterQueryExecutor.inDatabaseFolder( | |
path: 'test.db', logStatements: true)); | |
@override | |
int get schemaVersion => 1; | |
Stream<List<Room>> get watchAll => select(rooms).watch(); | |
Future addRoom(String desc) { | |
return into(rooms).insert(RoomsCompanion(desc: Value(desc))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
import 'database.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Provider( | |
builder: (_) => MyDatabase(), | |
child: MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: RoomsList(), | |
), | |
); | |
} | |
} | |
class RoomsList extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final db = Provider.of<MyDatabase>(context); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Rooms'), | |
), | |
body: Column( | |
children: <Widget>[ | |
StreamBuilder<List<Room>>( | |
stream: db.watchAll, | |
builder: (ctx, snapshot) { | |
print(snapshot); | |
return Text('There are ${snapshot?.data?.length} rooms'); | |
}, | |
), | |
RaisedButton( | |
child: Text('Another page'), | |
onPressed: () { | |
Navigator.of(context) | |
.push(MaterialPageRoute(builder: (_) => NewPage())); | |
}, | |
), | |
], | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () => db.addRoom('test'), | |
), | |
); | |
} | |
} | |
class NewPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
final db = Provider.of<MyDatabase>(context); | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Another'), | |
), | |
body: StreamBuilder<List<Room>>( | |
stream: db.watchAll, | |
builder: (ctx, snapshot) { | |
print('In another page: ' + snapshot.toString()); | |
return Text('There are ${snapshot?.data?.length} rooms'); | |
}, | |
), | |
floatingActionButton: FloatingActionButton( | |
child: Icon(Icons.add), | |
onPressed: () => db.addRoom('test'), | |
), | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: moor_test | |
description: A new Flutter project. | |
version: 1.0.0+1 | |
environment: | |
sdk: ">=2.1.0 <3.0.0" | |
dependencies: | |
moor_flutter: ^1.5.0 | |
provider: | |
flutter: | |
sdk: flutter | |
dev_dependencies: | |
moor_generator: ^1.5.0 | |
build_runner: any | |
flutter_test: | |
sdk: flutter | |
dependency_overrides: | |
moor: | |
git: | |
url: https://github.com/simolus3/moor.git | |
ref: develop | |
path: moor/ | |
flutter: | |
uses-material-design: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment