fix(crud): update example
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:crud_bloc_example/models.dart';
|
||||
import 'package:crud_bloc_example/user_cubit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:wyatt_crud_bloc/wyatt_crud_bloc.dart';
|
||||
@@ -27,17 +28,16 @@ class MyApp extends StatelessWidget {
|
||||
// This widget is the root of your application.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final _userRepository = CrudRepositoryFirestore<UserFirestore>(
|
||||
'users_crud',
|
||||
UserFirestore.parser(),
|
||||
);
|
||||
final CrudDataSource<User> userLocalDataSource =
|
||||
CrudInMemoryDataSourceImpl<User>(toMap: (user) => user.toMap());
|
||||
|
||||
final _userCubit = CrudCubit<UserFirestore>(_userRepository);
|
||||
final CrudRepository<User> userRepository =
|
||||
CrudRepositoryImpl(crudDataSource: userLocalDataSource);
|
||||
|
||||
return RepositoryProvider<CrudRepositoryFirestore<UserFirestore>>(
|
||||
create: (context) => _userRepository,
|
||||
child: BlocProvider<CrudCubit<UserFirestore>>(
|
||||
create: (context) => _userCubit,
|
||||
return RepositoryProvider<CrudRepository<User>>.value(
|
||||
value: userRepository,
|
||||
child: BlocProvider<UserCubit>(
|
||||
create: (context) => UserCubit(userRepository)..getAll(),
|
||||
child: MaterialApp(
|
||||
title: 'Flutter Demo',
|
||||
theme: ThemeData(
|
||||
@@ -64,54 +64,42 @@ class MyHomePage extends StatelessWidget {
|
||||
children: [
|
||||
const SizedBox(height: 20),
|
||||
const Text("Data:"),
|
||||
CrudStreamBuilder<UserFirestore>(
|
||||
onError: (context, state) => const SizedBox.shrink(),
|
||||
onLoading: (context, state) => const Text("Loading..."),
|
||||
onStream: (context, data) {
|
||||
return ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: data.length,
|
||||
itemBuilder: (context, index) {
|
||||
final user = data.elementAt(index);
|
||||
return ListTile(
|
||||
title: Text(user?.name ?? 'Error'),
|
||||
subtitle: Text(user?.id ?? 'Error'),
|
||||
onTap: () {
|
||||
context.read<CrudCubit<UserFirestore>>().get(
|
||||
(user?.id)!,
|
||||
);
|
||||
},
|
||||
onLongPress: () {
|
||||
context.read<CrudCubit<UserFirestore>>().delete(
|
||||
(user?.id)!,
|
||||
);
|
||||
BlocBuilder<UserCubit, CrudState>(
|
||||
builder: (context, state) {
|
||||
return CrudBuilder.typed<CrudListLoaded<User?>>(
|
||||
state: state,
|
||||
builder: ((context, state) {
|
||||
return ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: state.data.length,
|
||||
itemBuilder: (context, index) {
|
||||
final user = state.data.elementAt(index);
|
||||
return ListTile(
|
||||
title: Text(user?.name ?? 'Error'),
|
||||
subtitle: Text(user?.id ?? 'Error'),
|
||||
onTap: () {
|
||||
context.read<UserCubit>().delete(
|
||||
(user?.id)!,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
BlocBuilder<CrudCubit<UserFirestore>, CrudState<UserFirestore>>(
|
||||
builder: (context, state) {
|
||||
return Center(
|
||||
child: Text(
|
||||
state.toString(),
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(
|
||||
fontSize: 20,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
}),
|
||||
initialBuilder: (context, state) => const Text("Loading..."),
|
||||
loadingBuilder: (context, state) => const Text("Loading..."),
|
||||
errorBuilder: (context, state) => Text("Error: $state"),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: 20),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
final r = Random().nextInt(1000);
|
||||
context.read<CrudCubit<UserFirestore>>().create(
|
||||
UserFirestore(
|
||||
context.read<UserCubit>().create(
|
||||
User(
|
||||
id: '$r',
|
||||
name: 'Wyatt $r',
|
||||
email: '$r@wyattapp.io',
|
||||
phone: '06$r'),
|
||||
@@ -121,44 +109,24 @@ class MyHomePage extends StatelessWidget {
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<CrudCubit<UserFirestore>>().deleteAll();
|
||||
context.read<UserCubit>().deleteAll();
|
||||
},
|
||||
child: const Text("DeleteAll"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<CrudCubit<UserFirestore>>().getAll();
|
||||
context.read<UserCubit>().getAll();
|
||||
},
|
||||
child: const Text("GetAll"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context
|
||||
.read<CrudCubit<UserFirestore>>()
|
||||
.query([LimitQueryFirestore(1)]);
|
||||
.read<UserCubit>()
|
||||
.query([LimitQuery(2)]);
|
||||
},
|
||||
child: const Text("Query"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<CrudCubit<UserFirestore>>().streamOf();
|
||||
},
|
||||
child: const Text("StreamOf"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context
|
||||
.read<CrudCubit<UserFirestore>>()
|
||||
.updateAll({'updated': DateTime.now()});
|
||||
},
|
||||
child: const Text("UpdateAll"),
|
||||
),
|
||||
ElevatedButton(
|
||||
onPressed: () {
|
||||
context.read<CrudCubit<UserFirestore>>().reset();
|
||||
},
|
||||
child: const Text("Reset"),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class AppBlocObserver extends BlocObserver {
|
||||
final bool printEvent;
|
||||
final bool printError;
|
||||
final bool printChange;
|
||||
final bool printTransition;
|
||||
|
||||
AppBlocObserver({
|
||||
this.printEvent = true,
|
||||
this.printError = true,
|
||||
this.printTransition = true,
|
||||
this.printChange = true,
|
||||
});
|
||||
|
||||
@override
|
||||
void onEvent(Bloc<dynamic, dynamic> bloc, Object? event) {
|
||||
super.onEvent(bloc, event);
|
||||
if (printEvent) {
|
||||
debugPrint('onEvent(${bloc.runtimeType}, $event)');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(BlocBase<dynamic> bloc, Object error, StackTrace stackTrace) {
|
||||
if (printError) {
|
||||
debugPrint(
|
||||
'onError(${bloc.runtimeType}, $error, $stackTrace)',
|
||||
);
|
||||
}
|
||||
super.onError(bloc, error, stackTrace);
|
||||
}
|
||||
|
||||
@override
|
||||
void onChange(BlocBase<dynamic> bloc, Change<dynamic> change) {
|
||||
super.onChange(bloc, change);
|
||||
if (printChange) {
|
||||
debugPrint('onChange(${bloc.runtimeType}, $change)');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onTransition(
|
||||
Bloc<dynamic, dynamic> bloc,
|
||||
Transition<dynamic, dynamic> transition,
|
||||
) {
|
||||
super.onTransition(bloc, transition);
|
||||
if (printTransition) {
|
||||
debugPrint('onTransition(${bloc.runtimeType}, $transition)');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,15 +15,13 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:crud_bloc_example/app.dart';
|
||||
import 'package:crud_bloc_example/firebase_options.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:crud_bloc_example/app_bloc_observer.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
Bloc.observer = AppBlocObserver();
|
||||
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
@@ -14,10 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:wyatt_crud_bloc/wyatt_crud_bloc.dart';
|
||||
|
||||
class UserFirestore extends Model<DocumentSnapshot, UserFirestore> {
|
||||
class User extends ObjectModel {
|
||||
@override
|
||||
String? id;
|
||||
|
||||
@@ -25,33 +24,32 @@ class UserFirestore extends Model<DocumentSnapshot, UserFirestore> {
|
||||
String? email;
|
||||
String? phone;
|
||||
|
||||
UserFirestore({
|
||||
User({
|
||||
required this.name,
|
||||
required this.email,
|
||||
required this.phone,
|
||||
this.id,
|
||||
});
|
||||
UserFirestore._();
|
||||
// User._();
|
||||
|
||||
factory UserFirestore.parser() {
|
||||
return UserFirestore._();
|
||||
}
|
||||
// factory User.parser() {
|
||||
// return User._();
|
||||
// }
|
||||
|
||||
@override
|
||||
UserFirestore? from(DocumentSnapshot? object) {
|
||||
if (object == null) return null;
|
||||
if (object.exists) {
|
||||
return UserFirestore(
|
||||
id: object.id,
|
||||
name: (object.data() as Map<String, dynamic>?)!['name'] as String,
|
||||
email: (object.data() as Map<String, dynamic>?)!['email'] as String,
|
||||
phone: (object.data() as Map<String, dynamic>?)!['phone'] as String,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// @override
|
||||
// User? from(DocumentSnapshot? object) {
|
||||
// if (object == null) return null;
|
||||
// if (object.exists) {
|
||||
// return User(
|
||||
// id: object.id,
|
||||
// name: (object.data() as Map<String, dynamic>?)!['name'] as String,
|
||||
// email: (object.data() as Map<String, dynamic>?)!['email'] as String,
|
||||
// phone: (object.data() as Map<String, dynamic>?)!['phone'] as String,
|
||||
// );
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
@override
|
||||
Map<String, Object> toMap() {
|
||||
return {
|
||||
'name': name ?? '',
|
||||
@@ -62,5 +60,5 @@ class UserFirestore extends Model<DocumentSnapshot, UserFirestore> {
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
'UserFirestore(id: $id, name: $name, email: $email, phone: $phone)';
|
||||
'User(id: $id, name: $name, email: $email, phone: $phone)';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
import 'package:crud_bloc_example/models.dart';
|
||||
import 'package:wyatt_crud_bloc/wyatt_crud_bloc.dart';
|
||||
|
||||
class UserCubit extends CrudCubit<User> {
|
||||
final CrudRepository<User> _crudRepository;
|
||||
|
||||
UserCubit(this._crudRepository);
|
||||
|
||||
@override
|
||||
Create<User>? get crudCreate => Create(_crudRepository);
|
||||
|
||||
@override
|
||||
Delete<User>? get crudDelete => Delete(_crudRepository);
|
||||
|
||||
@override
|
||||
DeleteAll<User>? get crudDeleteAll => DeleteAll(_crudRepository);
|
||||
|
||||
@override
|
||||
Get<User>? get crudGet => Get(_crudRepository);
|
||||
|
||||
@override
|
||||
GetAll<User>? get crudGetAll => GetAll(_crudRepository);
|
||||
|
||||
@override
|
||||
Query<User>? get crudQuery => Query(_crudRepository);
|
||||
|
||||
@override
|
||||
Update<User>? get crudUpdate => Update(_crudRepository);
|
||||
|
||||
@override
|
||||
UpdateAll<User>? get crudUpdateAll => UpdateAll(_crudRepository);
|
||||
}
|
||||
Reference in New Issue
Block a user