问题描述
我已经按照以下方式定义了一个抽象的无状态小部件
abstract class BaseStatelessWidget<T extends Bloc> extends StatelessWidget {
@override
Widget build(BuildContext context) {
if (shouldHideStatusBar) {
SystemChrome.setEnabledsystemUIOverlays([]);
}
return MultiBlocProvider(
providers: [
BlocProvider(
create: (context) {
BaseBloc baseBloc = getIt<BaseBloc>();
baseBloc.add(BaseEvent.networkListeningInitiated());
return baseBloc;
},),BlocProvider(
create: (context) => getImplementedBloc(context),],child: BlocListener<BaseBloc,BaseState>(
listener: _handleState,child: buildScreen(context),);
}
bool get shouldHideStatusBar => false;
_handleState(BuildContext context,BaseState state) {
// here different state will be managed
}
Widget buildScreen(BuildContext context);
T getImplementedBloc(BuildContext context);
}
另一个无状态小部件SigninPage按照以下方式进行扩展
class SigninPage extends BaseStatelessWidget<SigninBloc> {
@override
Widget buildScreen(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: SigninForm(),resizetoAvoidBottomInset: true,);
}
@override
bool get shouldHideStatusBar => true;
@override
SigninBloc getImplementedBloc(BuildContext context) {
return getIt<SigninBloc>();
}
}
出于这个问题,我从SingInForm类中删除了冗余代码,这里是代码:
class SigninForm extends StatelessWidget {
@override
Widget build(BuildContext context) {
return PrimaryButton(
btnText: getString(context,StringKeys.signin),onButtonClick: () => onSignin(context),);
}
onSignin(BuildContext context) {
context.bloc<BaseBloc>().add(BaseEvent.changeLoaderStatus(visible: true)); => this is not working I tried to check if it is null. this is not null
context.bloc<SigninBloc>().add(SigninEvent.loginWithEmailPassword()); => this is working fine
}
}
这是我定义的BaseBloc类。 mapEventToState()方法和onTransition()方法都不会被调用。
import 'dart:async';
import 'package:connectivity/connectivity.dart';
import 'package:Flutter_bloc/Flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';
import 'package:refyne_app/domain/core/i_network_aware_facade.dart';
import 'package:refyne_app/domain/core/logger.dart';
import 'package:refyne_app/infrastructure/core/api_service/connectivity_status.dart';
part 'base_event.dart';
part 'base_state.dart';
part 'base_bloc.freezed.dart';
@injectable
class BaseBloc extends Bloc<BaseEvent,BaseState> {
INetworkAwareFacade _networkHandlerFacade;
BaseBloc(this._networkHandlerFacade)
: super(BaseState.initial(
isLoaderVisible: false,));
@override
Stream<BaseState> mapEventToState(
BaseEvent event,) async* {
yield* event.map(
networkListeningInitiated: (_) => _handleNetworkChange(),<== This one is causing the issue,if i remove this method. It works fine.
networkListeningStopped: (_) async* {},changeLoaderStatus: (ChangeLoaderStatus value) async* {
logger.d('from bloc loader called with $value');
yield state.copyWith(isLoaderVisible: value.visible);
},);
}
Stream<BaseState> _handleNetworkChange() async* {
await for (ConnectivityResult result
in _networkHandlerFacade.onConnectionChange()) { <== onConnectionChange will give you a stream
if (result == ConnectivityResult.none) {
yield state.copyWith(
connectionStatus: ConnectionStatus(
type: ConnectionType.NONE,isworking: false,));
} else {
bool isworking = await _networkHandlerFacade.checkConnection();
ConnectionType type = result == ConnectivityResult.mobile
? ConnectionType.MOBILE
: ConnectionType.WIFI;
yield state.copyWith(
connectionStatus: ConnectionStatus(type: type,isworking: isworking),);
}
}
}
@override
void onError(Object error,StackTrace stackTrace) {
if (error is Exception) {
_handleException(error);
} else {
super.onError(error,stackTrace);
// Todo:: handle this exception scenario
}
}
_handleException(Exception e) {
print(e);
}
@override
void onTransition(Transition<BaseEvent,BaseState> transition) {
logger.d('from base bloc ${transition.event}');
logger.d('from base bloc ${transition.currentState}');
logger.d('from base bloc ${transition.nextState}');
super.onTransition(transition);
}
}
我正在尝试访问BaseStatelessWidget类中定义的基本块。我定义的子块工作正常,它也正在发送事件。我的问题是我无法更改父集团的状态。有人可以告诉我我在做什么错吗?任何帮助将不胜感激。谢谢...
解决方法
经过一天的苦苦挣扎,现在我明白了为什么会这样。我直接在mapEventToState方法中处理流。当我将流处理从mapEventToState移到构造函数时,它工作正常。 希望它能帮助某人...
有关更多信息,请参见此示例。