leancode_cubit_utils_cqrs 0.2.0 leancode_cubit_utils_cqrs: ^0.2.0 copied to clipboard
An extension of leancode_cubit_utils that provides cqrs support.
import 'dart:developer';
import 'package:cqrs/cqrs.dart';
import 'package:example/cqrs/cqrs.dart';
import 'package:example/pages/home_page.dart';
import 'package:example/pages/paginated/paginated_cubit_page.dart';
import 'package:example/pages/query/query_page.dart';
import 'package:flutter/material.dart';
import 'package:leancode_cubit_utils_cqrs/leancode_cubit_utils_cqrs.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';
class Routes {
static const home = '/';
static const simpleQuery = '/simple-query';
static const simpleQueryHook = '/simple-query-hook';
static const paginatedCubit = '/paginated-cubit';
}
void _setupLogger() {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen(
(record) => log(
record.message,
time: record.time,
sequenceNumber: record.sequenceNumber,
level: record.level.value,
name: record.loggerName,
zone: record.zone,
error: record.error,
stackTrace: record.stackTrace,
),
);
FlutterError.onError = (details) {
FlutterError.dumpErrorToConsole(details);
};
}
void main() {
final cqrs = createMockedCqrs();
_setupLogger();
runApp(
Provider<Cqrs>.value(
value: cqrs,
child: PaginatedLayoutConfigProvider(
onFirstPageLoading: (context) => const Center(
child: CircularProgressIndicator(),
),
onFirstPageError: (context, error, retry) => Error(
retry: retry,
error: error,
),
onNextPageLoading: (context) => const Center(
child: CircularProgressIndicator(),
),
onNextPageError: (context, error, retry) => Error(
retry: retry,
error: error,
),
onEmptyState: (context) => const Center(child: Text('No items')),
child: RequestLayoutConfigProvider(
requestMode: RequestMode.replace,
onLoading: (BuildContext context) =>
const CircularProgressIndicator(),
onError: (
BuildContext context,
RequestErrorState<dynamic, dynamic> error,
VoidCallback? onErrorCallback,
) =>
const Text(
'Error',
style: TextStyle(color: Colors.red),
),
child: const MainApp(),
),
),
),
);
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) => MaterialApp(
routes: <String, WidgetBuilder>{
Routes.home: (_) => const HomePage(),
Routes.simpleQuery: (_) => const QueryScreen(),
Routes.simpleQueryHook: (_) => const QueryHookScreen(),
Routes.paginatedCubit: (_) => const PaginatedCubitScreen(),
},
);
}
class Error extends StatelessWidget {
const Error({
super.key,
this.retry,
required this.error,
});
final VoidCallback? retry;
final Object? error;
@override
Widget build(BuildContext context) => retry != null
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(error.toString()),
ElevatedButton(onPressed: retry, child: const Text('Retry')),
],
)
: Text(error.toString());
}