-
Notifications
You must be signed in to change notification settings - Fork 1
/
one_to_many.dart
244 lines (225 loc) · 6.87 KB
/
one_to_many.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import 'dart:math';
import 'package:example/eloquents/car.dart';
import 'package:example/eloquents/user.dart';
import 'package:example/models/car.dart';
import 'package:example/models/user.dart';
import 'package:flutter/material.dart';
import 'package:wazeloquent/wazeloquent.dart';
class OneToManyWidget extends StatefulWidget {
const OneToManyWidget({Key? key}) : super(key: key);
@override
State<OneToManyWidget> createState() => _OneToManyWidgetState();
}
const carNames = [
'Lamborghini Diablo',
'Ford Raptor',
'Ferrari Testarossa',
'Porsche 911 Carrera',
'Jeep Gladiator'
];
class _OneToManyWidgetState extends State<OneToManyWidget> {
final CarEloquent carEloquent = CarEloquent();
final UserEloquent userEloquent = UserEloquent();
List<User> users = [];
List<Car> cars = [];
@override
void initState() {
loadUsers();
loadCars();
super.initState();
}
showSnack(String message, {Duration? duration}) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(message),
duration: duration ?? const Duration(milliseconds: 600),
));
}
loadCars() async {
var data = await carEloquent.all();
updateCarState(data);
}
loadUsers() async {
var data = await userEloquent.all();
updateUserState(data);
}
updateUserState(List<Map<String, Object?>>? data) async {
if (data == null) {
showSnack('Empty');
}
users = [];
for (var user in data!) {
users.add(await User.withCars(user));
}
setState(() {
users = users;
});
}
updateCarState(List<Map<String, Object?>>? data) async {
if (data == null) {
showSnack('Empty');
}
cars = [];
for (var car in data!) {
cars.add(await Car.withUser(car));
}
setState(() {
cars = cars;
});
}
create() async {
if (users.isEmpty) {
showSnack('Empty User');
return;
}
var data = await carEloquent.create({
'name': carNames[Random().nextInt(carNames.length)],
'user_id': users[Random().nextInt(users.length)].id,
'createdAt': DateTime.now().toIso8601String(),
'updatedAt': DateTime.now().toIso8601String()
});
showSnack('Created id - $data');
loadUsers();
}
filterCars() async {
int index = Random().nextInt(users.length);
User user = users[index];
showSnack(
'Filtering for cars which ${user.name} owns ... ',
);
var query = await user.getCars();
var data =
await query.where('name', '%Ford%', operator: Operator.like).get();
if (data != null && data.isNotEmpty) {
List<Car> _cars = [];
for (var car in data) {
_cars.add(Car.fromDB(car));
}
setState(() {
users[index].cars = _cars;
});
}
}
delete() async {
if (cars.isEmpty) {
showSnack('Empty car');
return;
}
var data = await carEloquent.where('id', cars.first.id.toString()).delete();
showSnack('Delete rows - $data');
loadCars();
}
search() async {
if (users.isEmpty) {
showSnack('Empty user');
}
User user = users[Random().nextInt(users.length)];
showSnack('Searching for "F" cars which ${user.name} owns ... ',
duration: const Duration(milliseconds: 1500));
var data = await (await user.getCars()).search('F');
List<String> searchCarIds = data.map((e) => e['id'].toString()).toList();
showSnack('Search rows - ${data.length}');
List<User> temp = [];
for (var user in users) {
if (user.cars.isNotEmpty &&
user.cars
.where((element) => searchCarIds.contains(element.id.toString()))
.isNotEmpty) {
temp.add(user);
}
}
setState(() {
users = temp;
});
}
orderDesc() async {
User user = users[Random().nextInt(users.length)];
showSnack('Ordering desc for ${user.name}',
duration: const Duration(milliseconds: 2000));
var data = await (await user.getCars()).orderByDesc('name').get();
int index = users.indexOf(user);
List<Car> temp = [];
user.cars = [];
if (data != null) {
for (var car in data) {
temp.add(Car.fromDB(car));
}
}
user.cars = temp;
setState(() {
users[index] = user;
});
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return SingleChildScrollView(
controller: ScrollController(),
child: SizedBox(
width: size.width * 0.95,
height: size.height,
child: Column(children: [
const SizedBox(height: 20),
SizedBox(
height: size.height * 0.7,
width: size.width * 0.7,
child: ListView.builder(
itemCount: users.length,
itemBuilder: (_, int index) {
User user = users[index];
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
user.name,
style:
const TextStyle(color: Colors.blue, fontSize: 20),
),
const SizedBox(height: 10),
SizedBox(
height: user.cars.length * 60,
child: ListView.builder(
physics: const NeverScrollableScrollPhysics(),
itemCount: user.cars.length,
itemBuilder: (context, index) {
Car car = user.cars[index];
return ListTile(
title: Text('- ' + car.name),
);
}),
)
],
);
})),
SizedBox(
width: size.width * 0.7,
child: Wrap(
spacing: 8,
runSpacing: 8,
children: [
ElevatedButton(
onPressed: create, child: const Text('Create Car')),
ElevatedButton(
onPressed: delete, child: const Text('Delete Car')),
ElevatedButton(
onPressed: () {}, child: const Text('Update Car')),
ElevatedButton(
onPressed: filterCars, child: const Text('Filter Car')),
ElevatedButton(
onPressed: search,
child: const Text('Search cars via user')),
ElevatedButton(
onPressed: orderDesc, child: const Text('Order desc')),
ElevatedButton(
onPressed: loadUsers, child: const Text('Reload')),
],
),
)
]),
),
);
}
}