From df6eab246287717b1ebe50155c2504e7d6b914d9 Mon Sep 17 00:00:00 2001 From: westito Date: Sun, 19 Jan 2025 18:06:22 +0100 Subject: [PATCH 1/3] Update bloc to v9.0.0 --- example/pubspec.yaml | 12 ++++++------ pubspec.yaml | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 52c3f05..62764e0 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -31,11 +31,11 @@ dependencies: sdk: flutter hooked_bloc: path: '../' - get_it: 7.2.0 - injectable: 2.2.0 + get_it: ^8.0.3 + injectable: ^2.5.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. - cupertino_icons: ^1.0.4 + cupertino_icons: ^1.0.8 dev_dependencies: flutter_test: @@ -46,9 +46,9 @@ dev_dependencies: # activated in the `analysis_options.yaml` file located at the root of your # package. See that file for information about deactivating specific lint # rules and activating additional ones. - flutter_lints: ^2.0.1 - injectable_generator: 2.3.0 - build_runner: ^2.1.11 + flutter_lints: ^5.0.0 + injectable_generator: ^2.7.0 + build_runner: ^2.4.14 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/pubspec.yaml b/pubspec.yaml index b9533c5..f1ba995 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,16 +13,16 @@ environment: dependencies: flutter: sdk: flutter - flutter_bloc: ^8.1.3 - bloc: ^8.1.2 - flutter_hooks: ^0.20.1 - meta: ^1.10.0 - provider: ^6.0.5 + flutter_bloc: ^9.0.0 + bloc: ^9.0.0 + flutter_hooks: ^0.20.5 + meta: ^1.15.0 + provider: ^6.1.2 dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^3.0.1 - mocktail: ^1.0.2 + flutter_lints: ^5.0.0 + mocktail: ^1.0.4 flutter: From f1a1a697f363ecd5115e5835349c8ac863a36c33 Mon Sep 17 00:00:00 2001 From: westito Date: Sun, 19 Jan 2025 18:14:31 +0100 Subject: [PATCH 2/3] Fix analyze issues --- analysis_options.yaml | 1 + example/lib/main.dart | 2 +- example/lib/page/home_page.dart | 4 ++-- example/lib/page/real_life_hook_page.dart | 2 +- example/lib/page/real_life_page.dart | 2 +- example/lib/page/use_action_listener_page.dart | 2 +- example/lib/page/use_bloc_builder_page.dart | 2 +- example/lib/page/use_bloc_comparative_builder_page.dart | 2 +- example/lib/page/use_bloc_comparative_listener_page.dart | 2 +- example/lib/page/use_bloc_listener_page.dart | 2 +- example/lib/page/use_bloc_page.dart | 2 +- example/lib/widget/clickable_item_list.dart | 4 ++-- example/lib/widget/fab_actions_scaffold.dart | 4 ++-- example/lib/widget/item_detail.dart | 4 ++-- example/lib/widget/message_bottom_sheet_content.dart | 2 +- example/pubspec.yaml | 2 ++ lib/hooked_bloc.dart | 2 +- lib/src/bloc_factory.dart | 2 +- lib/src/bloc_hook.dart | 2 +- 19 files changed, 24 insertions(+), 21 deletions(-) diff --git a/analysis_options.yaml b/analysis_options.yaml index 2d56355..bddfac2 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -5,3 +5,4 @@ analyzer: body_might_complete_normally_nullable: ignore depend_on_referenced_packages: ignore no_leading_underscores_for_library_prefixes: ignore + use_build_context_synchronously: ignore diff --git a/example/lib/main.dart b/example/lib/main.dart index 0f29cd8..6482677 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -49,7 +49,7 @@ void main() async { // } class MyApp extends StatelessWidget { - const MyApp({Key? key}) : super(key: key); + const MyApp({super.key}); final appName = "Hooked Bloc"; diff --git a/example/lib/page/home_page.dart b/example/lib/page/home_page.dart index 57b6607..0dd9996 100644 --- a/example/lib/page/home_page.dart +++ b/example/lib/page/home_page.dart @@ -10,9 +10,9 @@ import 'use_bloc_page.dart'; class HomePage extends StatelessWidget { const HomePage({ - Key? key, + super.key, required this.title, - }) : super(key: key); + }); final String title; diff --git a/example/lib/page/real_life_hook_page.dart b/example/lib/page/real_life_hook_page.dart index ebab511..10c086d 100644 --- a/example/lib/page/real_life_hook_page.dart +++ b/example/lib/page/real_life_hook_page.dart @@ -6,7 +6,7 @@ import '../widget/clickable_item_list.dart'; import '../widget/item_detail.dart'; class RealLifeHookPage extends HookWidget { - const RealLifeHookPage({Key? key}) : super(key: key); + const RealLifeHookPage({super.key}); @override Widget build(BuildContext context) { diff --git a/example/lib/page/real_life_page.dart b/example/lib/page/real_life_page.dart index 1a4de91..4c4147c 100644 --- a/example/lib/page/real_life_page.dart +++ b/example/lib/page/real_life_page.dart @@ -7,7 +7,7 @@ import '../widget/clickable_item_list.dart'; import '../widget/item_detail.dart'; class RealLifePage extends StatelessWidget { - const RealLifePage({Key? key}) : super(key: key); + const RealLifePage({super.key}); @override Widget build(BuildContext context) { diff --git a/example/lib/page/use_action_listener_page.dart b/example/lib/page/use_action_listener_page.dart index d165491..755b134 100644 --- a/example/lib/page/use_action_listener_page.dart +++ b/example/lib/page/use_action_listener_page.dart @@ -9,7 +9,7 @@ import '../widget/message_bottom_sheet_content.dart'; // The page must inherit from HookWidget class UseActionListenerPage extends HookWidget { - UseActionListenerPage({Key? key}) : super(key: key); + UseActionListenerPage({super.key}); final MessageActionCubit cubit = MessageActionCubit(); diff --git a/example/lib/page/use_bloc_builder_page.dart b/example/lib/page/use_bloc_builder_page.dart index 0811c02..7c5cc54 100644 --- a/example/lib/page/use_bloc_builder_page.dart +++ b/example/lib/page/use_bloc_builder_page.dart @@ -5,7 +5,7 @@ import 'package:flutter_hooks/flutter_hooks.dart'; // The page must inherit from HookWidget class UseBlocBuilderPage extends HookWidget { - UseBlocBuilderPage({Key? key}) : super(key: key); + UseBlocBuilderPage({super.key}); final CounterCubit cubit = CounterCubit("My cubit"); diff --git a/example/lib/page/use_bloc_comparative_builder_page.dart b/example/lib/page/use_bloc_comparative_builder_page.dart index d2568c3..c12a77a 100644 --- a/example/lib/page/use_bloc_comparative_builder_page.dart +++ b/example/lib/page/use_bloc_comparative_builder_page.dart @@ -4,7 +4,7 @@ import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:hooked_bloc/hooked_bloc.dart'; class UseBlocComparativeBuilderPage extends HookWidget { - UseBlocComparativeBuilderPage({Key? key}) : super(key: key); + UseBlocComparativeBuilderPage({super.key}); final CounterCubit cubit = CounterCubit("My cubit"); diff --git a/example/lib/page/use_bloc_comparative_listener_page.dart b/example/lib/page/use_bloc_comparative_listener_page.dart index 158dc93..0a14c1e 100644 --- a/example/lib/page/use_bloc_comparative_listener_page.dart +++ b/example/lib/page/use_bloc_comparative_listener_page.dart @@ -6,7 +6,7 @@ import 'package:hooked_bloc/hooked_bloc.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; class UseBlocComparativeListenerPage extends HookWidget { - UseBlocComparativeListenerPage({Key? key}) : super(key: key); + UseBlocComparativeListenerPage({super.key}); final EventCubit cubit = EventCubit(); diff --git a/example/lib/page/use_bloc_listener_page.dart b/example/lib/page/use_bloc_listener_page.dart index 58a7bfa..61cfa37 100644 --- a/example/lib/page/use_bloc_listener_page.dart +++ b/example/lib/page/use_bloc_listener_page.dart @@ -6,7 +6,7 @@ import 'package:hooked_bloc/hooked_bloc.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; class UseBlocListenerPage extends HookWidget { - UseBlocListenerPage({Key? key}) : super(key: key); + UseBlocListenerPage({super.key}); final EventCubit cubit = EventCubit(); diff --git a/example/lib/page/use_bloc_page.dart b/example/lib/page/use_bloc_page.dart index e3cde18..b74ca4b 100644 --- a/example/lib/page/use_bloc_page.dart +++ b/example/lib/page/use_bloc_page.dart @@ -5,7 +5,7 @@ import 'package:hooked_bloc/hooked_bloc.dart'; // The page must inherit from HookWidget class UseBlocPage extends HookWidget { - const UseBlocPage({Key? key}) : super(key: key); + const UseBlocPage({super.key}); @override Widget build(BuildContext context) { diff --git a/example/lib/widget/clickable_item_list.dart b/example/lib/widget/clickable_item_list.dart index 4d36ad7..5c1f912 100644 --- a/example/lib/widget/clickable_item_list.dart +++ b/example/lib/widget/clickable_item_list.dart @@ -2,10 +2,10 @@ import 'package:flutter/material.dart'; class ClickableItemList extends StatelessWidget { const ClickableItemList({ - Key? key, + super.key, required this.itemCallback, required this.data, - }) : super(key: key); + }); final Function(int) itemCallback; final List data; diff --git a/example/lib/widget/fab_actions_scaffold.dart b/example/lib/widget/fab_actions_scaffold.dart index 21d98dc..d0816e6 100644 --- a/example/lib/widget/fab_actions_scaffold.dart +++ b/example/lib/widget/fab_actions_scaffold.dart @@ -2,12 +2,12 @@ import 'package:flutter/material.dart'; class FabActionsScaffold extends StatelessWidget { const FabActionsScaffold({ - Key? key, + super.key, required this.title, required this.count, required this.incrementCallback, required this.messageCallback, - }) : super(key: key); + }); final String title; final int count; diff --git a/example/lib/widget/item_detail.dart b/example/lib/widget/item_detail.dart index 748ffbd..cf72834 100644 --- a/example/lib/widget/item_detail.dart +++ b/example/lib/widget/item_detail.dart @@ -2,10 +2,10 @@ import 'package:flutter/material.dart'; class ItemDetail extends StatelessWidget { const ItemDetail({ - Key? key, + super.key, required this.index, required this.onClose, - }) : super(key: key); + }); final int index; final Function() onClose; diff --git a/example/lib/widget/message_bottom_sheet_content.dart b/example/lib/widget/message_bottom_sheet_content.dart index 33ece3f..01aadbe 100644 --- a/example/lib/widget/message_bottom_sheet_content.dart +++ b/example/lib/widget/message_bottom_sheet_content.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; class MessageBottomSheetContent extends StatelessWidget { - const MessageBottomSheetContent({Key? key, this.message}) : super(key: key); + const MessageBottomSheetContent({super.key, this.message}); final String? message; diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 62764e0..7797d62 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -37,6 +37,8 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 + flutter_bloc: any + flutter_hooks: any dev_dependencies: flutter_test: sdk: flutter diff --git a/lib/hooked_bloc.dart b/lib/hooked_bloc.dart index 796101a..10251f0 100644 --- a/lib/hooked_bloc.dart +++ b/lib/hooked_bloc.dart @@ -1,4 +1,4 @@ -library hooked_bloc; +library; export 'src/bloc_hook.dart'; export 'src/bloc_factory.dart'; diff --git a/lib/src/bloc_factory.dart b/lib/src/bloc_factory.dart index 00da9bf..9361a88 100644 --- a/lib/src/bloc_factory.dart +++ b/lib/src/bloc_factory.dart @@ -9,7 +9,7 @@ abstract class BlocFactory { typedef OnBlocCreated = Function(F factory); -/// Creates BlocBase class using provided factory. +/// Creates [BlocBase] class using provided factory. /// /// [useBlocFactory] will find [BlocFactory] class using injector provided by [BlocHookInjectionController] and then return [BlocBase] created by it. /// If [BlocHookInjectionController] has no provided injectors, [useBlocFactory] will look into widget tree and try find [BlocFactory] using [Provider.of] method diff --git a/lib/src/bloc_hook.dart b/lib/src/bloc_hook.dart index 14dc762..5288b4a 100644 --- a/lib/src/bloc_hook.dart +++ b/lib/src/bloc_hook.dart @@ -3,7 +3,7 @@ import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:hooked_bloc/src/bloc_defaults.dart'; import 'package:hooked_bloc/src/config/hooked_bloc_config.dart'; -/// Provides BlocBase class. +/// Provides [BlocBase] class. /// /// [useBloc] will find and return [BlocBase] class using injector provided by [BlocHookInjectionController] /// If [BlocHookInjectionController] has no provided injectors, [useBloc] will look into widget tree and try find BlocBase using [BlocProvider] From 6150170bcde7a93d3b92389e6e73be7534f324ae Mon Sep 17 00:00:00 2001 From: westito Date: Sun, 19 Jan 2025 18:14:55 +0100 Subject: [PATCH 3/3] Reformat code --- lib/src/bloc_action_listener.dart | 3 +- test/bloc_action_listener_test.dart | 98 ++++++++++++++--------------- 2 files changed, 49 insertions(+), 52 deletions(-) diff --git a/lib/src/bloc_action_listener.dart b/lib/src/bloc_action_listener.dart index ad60e63..12fe1f9 100644 --- a/lib/src/bloc_action_listener.dart +++ b/lib/src/bloc_action_listener.dart @@ -14,7 +14,8 @@ void useActionListener( useEffect( () { final subscription = actionMixin.actions.listen((action) { - if(actionWhen == null || actionWhen(actionMixin.previousAction, action)) { + if (actionWhen == null || + actionWhen(actionMixin.previousAction, action)) { onAction(action); } }); diff --git a/test/bloc_action_listener_test.dart b/test/bloc_action_listener_test.dart index 8570af7..a426abe 100644 --- a/test/bloc_action_listener_test.dart +++ b/test/bloc_action_listener_test.dart @@ -90,56 +90,52 @@ void main() { testWidgets( 'when Cubit dispatch multiple times, actionWhen should be called only with 2 chars', - (tester) async { - int listenerCalls = 0; - - Widget Function(BuildContext) builder( - ActionCubit cubit, - ) { - return (context) { - useActionListener( - cubit, - (ACTION action) { - listenerCalls++; - }, - actionWhen: (previousAction, action) => action.length == 2 - ); - - return Container(); - }; - } - - CounterActionCubit cubit = CounterActionCubit(); - - HookBuilder hookWidget = HookBuilder(builder: builder(cubit)); - - cubit.dispatchActionValue('1'); - await tester.pumpWidget(hookWidget); - expect(listenerCalls, 0); - - cubit.dispatchActionValue('11'); - await tester.pumpWidget(hookWidget); - expect(listenerCalls, 1); - - cubit.dispatchActionValue('2'); - await tester.pumpWidget(hookWidget); - expect(listenerCalls, 1); - - cubit.dispatchActionValue('22'); - await tester.pumpWidget(hookWidget); - expect(listenerCalls, 2); - - cubit.dispatchActionValue('333'); - await tester.pumpWidget(hookWidget); - expect(listenerCalls, 2); - - cubit.dispatchActionValue('4'); - await tester.pumpWidget(hookWidget); - expect(listenerCalls, 2); - - cubit.dispatchActionValue('55'); - await tester.pumpWidget(hookWidget); - expect(listenerCalls, 3); - }); + (tester) async { + int listenerCalls = 0; + + Widget Function(BuildContext) builder( + ActionCubit cubit, + ) { + return (context) { + useActionListener(cubit, (ACTION action) { + listenerCalls++; + }, actionWhen: (previousAction, action) => action.length == 2); + + return Container(); + }; + } + + CounterActionCubit cubit = CounterActionCubit(); + + HookBuilder hookWidget = HookBuilder(builder: builder(cubit)); + + cubit.dispatchActionValue('1'); + await tester.pumpWidget(hookWidget); + expect(listenerCalls, 0); + + cubit.dispatchActionValue('11'); + await tester.pumpWidget(hookWidget); + expect(listenerCalls, 1); + + cubit.dispatchActionValue('2'); + await tester.pumpWidget(hookWidget); + expect(listenerCalls, 1); + + cubit.dispatchActionValue('22'); + await tester.pumpWidget(hookWidget); + expect(listenerCalls, 2); + + cubit.dispatchActionValue('333'); + await tester.pumpWidget(hookWidget); + expect(listenerCalls, 2); + + cubit.dispatchActionValue('4'); + await tester.pumpWidget(hookWidget); + expect(listenerCalls, 2); + + cubit.dispatchActionValue('55'); + await tester.pumpWidget(hookWidget); + expect(listenerCalls, 3); + }); }); }