Skip to content

Commit

Permalink
refactor(flutter_login): use RepositoryProvider + dispose (#4365)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Mar 1, 2025
1 parent 4c1fe8f commit 1c0c213
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 44 deletions.
39 changes: 12 additions & 27 deletions examples/flutter_login/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,24 @@ import 'package:flutter_login/login/login.dart';
import 'package:flutter_login/splash/splash.dart';
import 'package:user_repository/user_repository.dart';

class App extends StatefulWidget {
class App extends StatelessWidget {
const App({super.key});

@override
State<App> createState() => _AppState();
}

class _AppState extends State<App> {
late final AuthenticationRepository _authenticationRepository;
late final UserRepository _userRepository;

@override
void initState() {
super.initState();
_authenticationRepository = AuthenticationRepository();
_userRepository = UserRepository();
}

@override
void dispose() {
_authenticationRepository.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return RepositoryProvider.value(
value: _authenticationRepository,
return MultiRepositoryProvider(
providers: [
RepositoryProvider(
create: (_) => AuthenticationRepository(),
dispose: (repository) => repository.dispose(),
),
RepositoryProvider(create: (_) => UserRepository()),
],
child: BlocProvider(
lazy: false,
create: (_) => AuthenticationBloc(
authenticationRepository: _authenticationRepository,
userRepository: _userRepository,
create: (context) => AuthenticationBloc(
authenticationRepository: context.read<AuthenticationRepository>(),
userRepository: context.read<UserRepository>(),
)..add(AuthenticationSubscriptionRequested()),
child: const AppView(),
),
Expand Down
8 changes: 1 addition & 7 deletions packages/flutter_bloc/lib/flutter_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ library flutter_bloc;

export 'package:bloc/bloc.dart';
export 'package:provider/provider.dart'
show
Create,
Dispose,
ProviderNotFoundException,
ReadContext,
SelectContext,
WatchContext;
show ProviderNotFoundException, ReadContext, SelectContext, WatchContext;

export './src/bloc_builder.dart';
export './src/bloc_consumer.dart';
Expand Down
10 changes: 5 additions & 5 deletions packages/flutter_bloc/lib/src/bloc_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';

/// {@template bloc_provider}
/// Takes a [Create] function that is responsible for
/// Takes a `create` function that is responsible for
/// creating the [Bloc] or [Cubit] and a [child] which will have access
/// to the instance via `BlocProvider.of(context)`.
/// It is used as a dependency injection (DI) widget so that a single instance
Expand All @@ -19,7 +19,7 @@ import 'package:provider/single_child_widget.dart';
/// ```
///
/// It automatically handles closing the instance when used with [Create].
/// By default, [Create] is called only when the instance is accessed.
/// By default, `create` is called only when the instance is accessed.
/// To override this behavior, set [lazy] to `false`.
///
/// ```dart
Expand All @@ -35,7 +35,7 @@ class BlocProvider<T extends StateStreamableSource<Object?>>
extends SingleChildStatelessWidget {
/// {@macro bloc_provider}
const BlocProvider({
required Create<T> create,
required T Function(BuildContext context) create,
Key? key,
this.child,
this.lazy = true,
Expand All @@ -52,7 +52,7 @@ class BlocProvider<T extends StateStreamableSource<Object?>>
///
/// A new [Bloc] or [Cubit] should not be created in `BlocProvider.value`.
/// New instances should always be created using the
/// default constructor within the [Create] function.
/// default constructor within the `create` function.
///
/// ```dart
/// BlocProvider.value(
Expand All @@ -76,7 +76,7 @@ class BlocProvider<T extends StateStreamableSource<Object?>>
/// Defaults to `true`.
final bool lazy;

final Create<T>? _create;
final T Function(BuildContext context)? _create;

final T? _value;

Expand Down
8 changes: 4 additions & 4 deletions packages/flutter_bloc/lib/src/repository_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';

/// {@template repository_provider}
/// Takes a [Create] function that is responsible for creating the repository
/// Takes a `create` function that is responsible for creating the repository
/// and a `child` which will have access to the repository via
/// `RepositoryProvider.of(context)`.
/// It is used as a dependency injection (DI) widget so that a single instance
Expand All @@ -28,15 +28,15 @@ import 'package:provider/provider.dart';
class RepositoryProvider<T> extends Provider<T> {
/// {@macro repository_provider}
RepositoryProvider({
required Create<T> create,
Dispose<T>? dispose,
required T Function(BuildContext context) create,
void Function(T value)? dispose,
Key? key,
Widget? child,
bool? lazy,
}) : super(
key: key,
create: create,
dispose: dispose,
dispose: (_, value) => dispose?.call(value),
child: child,
lazy: lazy,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_bloc/test/repository_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void main() {
home: Scaffold(
body: RepositoryProvider<Repository>(
create: (_) => const Repository(0),
dispose: (context, repository) {
dispose: (repository) {
disposeCalled = true;
expect(repository.data, equals(0));
},
Expand Down

0 comments on commit 1c0c213

Please sign in to comment.