Я создавал функцию входа / аутентификации, используя комбинацию этого руководства по входу в систему и руководств по чистой архитектуре ресокодера. Он на 99% работает отлично, но не реагирует должным образом на нажатие LoginButton
.
По какой-то причине, когда LoginBloc
вызывает AuthenticationBloc.add(loggedin())
, AuthenticationBloc прекрасно выдает состояние AuthenticationAuthenticated()
, но BlocBuilder
в Main.dart не получает изменения состояния. Даже OnTransition
внутри SimpleBlocDelegate срабатывает при выдаче AuthenticationAuthenticated
, но BlocBuilder
ничего не делает.
Main.dart
выглядит так:
import 'package:bloc/bloc.dart';
import 'package:flutter_app/dependency_injector.dart' as di;
import 'package:flutter_app/features/login/presentation/pages/login_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'features/login/presentation/bloc/user_login_bloc.dart';
import 'features/login/presentation/bloc/user_login_events.dart';
import 'features/login/presentation/bloc/user_login_states.dart';
class SimpleBlocDelegate extends BlocDelegate {
@override
void onEvent(Bloc bloc, Object event) {
print(event);
super.onEvent(bloc, event);
}
@override
void onTransition(Bloc bloc, Transition transition) {
print(transition);
super.onTransition(bloc, transition);
}
@override
void onError(Bloc bloc, Object error, StackTrace stackTrace) {
print(error);
super.onError(bloc, error, stackTrace);
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await di.init(); //Dependency Injection using get_it
BlocSupervisor.delegate = SimpleBlocDelegate();
runApp(
BlocProvider<UserAuthenticationBloc>(
create: (_) => sl<UserAuthenticationBloc>()..add(AppStarted()),
child: App(),
),
);
}
class App extends StatelessWidget {
App({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: BlocBuilder<UserAuthenticationBloc, AuthenticationState>(
builder: (context, state) {
if (state is AuthenticationAuthenticated) {
return Container(
child: HomePage(); // THIS NEVER HAPPENS, even though AuthBloc yields the State
}
if (state is AuthenticationUnauthenticated) {
return LoginScreen(); // THIS yeilds fine when AppStarted in passed on init.
}
if (state is AuthenticationLoading) {
return LoadingIndicator();
}
return Scaffold(
body: SplashPage();
)
},
),
);
}
}
Я могу только думать, что это как-то связано с get_it
. Внедрение зависимостей выглядит так:
final sl = GetIt.instance;
Future<void> init() async {
sl.registerFactory(
() => UserAuthenticationBloc(
getCachedUser: sl(),
),
);
sl.registerFactory(
() => LoginBloc(authenticationBloc: sl(), getUserFromEmailAndPassword: sl()),
);
...
}
а затем в дереве виджетов для loginscreen
создается LoginBloc
, поэтому он доступен для формы входа.
class LoginScreen extends StatelessWidget {
LoginScreen({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: BlocProvider<LoginBloc>(
create: (_) => sl<LoginBloc>(),
child: LoginForm(), //login form
),
);
}
}
ДВА РЕДАКТИРОВАНИЯ: 1. Я изменил UserAuthenticationBloc
в файле внедрения зависимостей с factory на lazysingleton ... теперь он работает. Однако я слышал, что использование синглтонов для классов с Streams может вызвать утечку памяти ?? Полагаю, это означает, что LoginBloc
не обращается к тому же экземпляру AuthBloc
, который слушает Main.dart? Я не знаю, как это сделать без синглтона ...
- Код UserAuthenticationBloc:
class UserAuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState> {
final GetCachedUser getCachedUser;
UserAuthenticationBloc({
@required GetCachedUser getCachedUser,
}) : assert(getCachedUser != null),
getCachedUser = getCachedUser;
@override
AuthenticationState get initialState => AuthenticationUninitialized();
@override
Stream<AuthenticationState> mapEventToState(AuthenticationEvent event) async* {
if (event is AppStarted) {
yield AuthenticationUnauthenticated();
}
}
if (event is LoggedIn) {
yield AuthenticationAuthenticated(); //this fires.
}
}
}
UserAuthenticationBloc
- person Sanjay Sharma   schedule 17.05.2020AuthenticationState
наследуетEquatable
? - person Sanjay Sharma   schedule 17.05.2020