Created
July 20, 2022 23:00
-
-
Save buraktabn/b44807daf19b15081dcb3b3f664aff22 to your computer and use it in GitHub Desktop.
Isar Link Demo
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:isar/isar.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
part 'main.g.dart'; | |
@Collection() | |
class Teacher { | |
int? id; | |
late String subject; | |
} | |
@Collection() | |
class Student { | |
int? id; | |
late String name; | |
final teachers = IsarLink<Teacher>(); | |
@override | |
String toString() { | |
return 'Student{id: $id, name: $name, teachers: ${teachers.value}}'; | |
} | |
} | |
late final Isar isar; | |
void main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
final dir = await getApplicationSupportDirectory(); | |
isar = await Isar.open( | |
schemas: [TeacherSchema, StudentSchema], | |
directory: dir.path, | |
); | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Isar Link Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Isar Link Demo'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _count = 0; | |
void _incrementCounter() async { | |
final count = _count++; | |
final mathTeacher = Teacher() | |
..id = 0 | |
..subject = 'Math $count'; | |
final linda = Student() | |
..id = 0 | |
..name = 'Linda $count' | |
..teachers.value = mathTeacher; | |
await isar.writeTxn((isar) async { | |
await isar.students.put(linda); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
actions: [ | |
IconButton( | |
onPressed: () => isar.writeTxn((isar) => isar.clear()).then((_) => _count = 0), | |
icon: const Icon(Icons.clear), | |
), | |
], | |
), | |
body: Center( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
StreamBuilder<Student?>( | |
stream: isar.students.watchObject(0, initialReturn: true), | |
builder: (context, snapshot) { | |
return Row( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
const Text('Student: '), | |
Text(snapshot.data?.toString() ?? 'No Data'), | |
], | |
); | |
}, | |
), | |
StreamBuilder<Teacher?>( | |
stream: isar.teachers.watchObject(0, initialReturn: true), | |
builder: (context, snapshot) { | |
return Row( | |
mainAxisSize: MainAxisSize.min, | |
children: [ | |
const Text('Teacher: '), | |
Text(snapshot.data?.toString() ?? 'No Data'), | |
], | |
); | |
}, | |
), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _incrementCounter, | |
tooltip: 'Increment', | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment