Flutter 为什么dart只想关闭我的一个集团?
我有两个集团Flutter 为什么dart只想关闭我的一个集团?,flutter,dart,bloc,flutter-bloc,Flutter,Dart,Bloc,Flutter Bloc,我有两个集团 imports ... part 'historical_events_event.dart'; part 'historical_events_state.dart'; class HistoricalEventsBloc extends Bloc<HistoricalEventsEvent, HistoricalEventsState> { final HistoryRepository historyRepository; StreamSubs
imports ...
part 'historical_events_event.dart';
part 'historical_events_state.dart';
class HistoricalEventsBloc
extends Bloc<HistoricalEventsEvent, HistoricalEventsState> {
final HistoryRepository historyRepository;
StreamSubscription _subscription;
HistoricalEventsBloc({
@required this.historyRepository,
}) : super(HistoricalEventsLoading());
@override
Stream<HistoricalEventsState> mapEventToState(
HistoricalEventsEvent event,
) async* {
if (event is HistoricalEventsRequested) {
yield HistoricalEventsLoading();
try {
await _subscription?.cancel();
_subscription = historyRepository.oldEvents.listen(
(QuerySnapshot _snapshot) => add(
_HistoricalEventsLoaded(_snapshot.oldEventsFromSnapshot),
),
);
} catch (error) {
yield HistoricalEventsError(message: error.toString());
}
}
if (event is _HistoricalEventsLoaded) {
yield HistoricalEventsLoaded(events: event.events);
}
if (event is DeleteHistoricalEvent) {
yield HistoricalEventsLoading();
try {
final deleted = await historyRepository.deleteHistoricalEvent(
eventId: event.eventToBeDeleted.id);
if (deleted) {
yield HistoricalEventsDeleted();
await _subscription?.cancel();
_subscription = historyRepository.oldEvents.listen(
(QuerySnapshot _snapshot) => add(
_HistoricalEventsLoaded(_snapshot.oldEventsFromSnapshot),
),
);
}
} catch (error) {
yield HistoricalEventsError(message: error.toString());
}
}
if (event is HistoricalEventDetailRequested) {
yield HistoricalEventsLoading();
try {
yield HistoricalEventDetail(sensors: event.sensors, event: event.event);
} catch (error) {
yield HistoricalEventsError(message: error.toString());
}
}
}
@override
Future<void> close() {
_subscription?.cancel();
return super.close();
}
}
导入。。。
“历史事件”部分;
“历史事件”和“国家灾难援助队”部分;
阶级历史集团
扩展集团{
最终历史存储库历史存储库;
流动认购(流动认购);;
历史文集({
@需要此.historyRepository,
}):super(historicaleventsload());
@凌驾
流映射事件状态(
历史事件,
)异步*{
if(请求历史事件){
屈服历史变量加载();
试一试{
等待订阅?取消();
_订阅=historyRepository.oldEvents.listen(
(QuerySnapshot\u快照)=>添加(
_已加载历史事件(_snapshot.oldEventsFromSnapshot),
),
);
}捕获(错误){
产生HistoricalEventError(消息:error.toString());
}
}
if(事件已加载){
生成HistoricalEventsLoaded(事件:event.events);
}
if(事件为DeleteHistoricalEvent){
屈服历史变量加载();
试一试{
最终删除=等待historyRepository.deleteHistoricalEvent(
eventId:event.eventToBeDeleted.id);
如果(已删除){
收益率历史要素删除();
等待订阅?取消();
_订阅=historyRepository.oldEvents.listen(
(QuerySnapshot\u快照)=>添加(
_已加载历史事件(_snapshot.oldEventsFromSnapshot),
),
);
}
}捕获(错误){
产生HistoricalEventError(消息:error.toString());
}
}
if(事件是历史记录的EventDetailRequested){
屈服历史变量加载();
试一试{
生成历史事件细节(传感器:event.sensors,事件:event.event);
}捕获(错误){
产生HistoricalEventError(消息:error.toString());
}
}
}
@凌驾
远期交割(){
_订阅?.cancel();
返回super.close();
}
}
及
导入。。。
“传感器事件.飞镖”部分;
“传感器状态.dart”部分;
类传感器sbloc扩展了Bloc{
最终传感器反向传感器反向传感器;
流动认购(流动认购);;
传感器SBLOC({
@需要这个。传感器还原,
}):super(传感器加载());
@凌驾
流映射事件状态(
第七次事件,
)异步*{
如果(传感器请求的事件){
屈服传感器加载();
试一试{
等待订阅?取消();
_订阅=sensorsRepository.sensors.listen(
(QuerySnapshot\u快照)=>添加(
_传感器已加载(_snapshot.sensorsFromSnapshot),
),
);
}捕获(错误){
产量传感器错误(消息:error.toString());
}
}
如果(事件为_传感器加载){
屈服传感器加载(传感器:事件传感器);
}
如果(事件是预先设定的){
屈服传感器加载();
试一试{
等待订阅?取消();
_订阅=sensorsRepository.sensors.listen(
(QuerySnapshot\u快照)=>添加(
_传感器爆炸(_snapshot.sensorsFromSnapshot),
),
);
}捕获(错误){
产量传感器错误(消息:error.toString());
}
}
如果(事件已被触发){
产量传感器编程(传感器:事件传感器);
}
如果(请求了事件){
屈服传感器加载();
试一试{
收益率初始值();
}捕获(错误){
产量传感器错误(消息:error.toString());
}
}
如果(事件为AddSensor){
屈服传感器加载();
试一试{
最终添加=等待传感器repository.addSensor(
id:event.id,
纬度:event.latitude,
经度:event.longitude,
地址:event.address,
);
如有(新增){
最终传感器addedSensor=等待传感器Repository.findSensorById(
id:event.id,
);
添加产量传感器(添加传感器:添加传感器);
等待订阅?取消();
_订阅=sensorsRepository.sensors.listen(
(QuerySnapshot\u快照)=>添加(
_传感器已加载(_snapshot.sensorsFromSnapshot),
),
);
}
}捕获(错误){
产量传感器错误(消息:error.toString());
}
}
如果(事件为UpdateSensorRequested){
屈服传感器加载();
试一试{
产量更新传感器初始值(
sensorToBeUpdated:event.sensorToBeUpdated,
isMap:event.isMap,
);
}捕获(错误){
产量传感器错误(消息:error.toString());
}
}
如果(事件为UpdateSensor){
屈服传感器加载();
试一试{
最终更新=等待传感器repository.updateSensor(
oldSensor:event.oldSensor,
id:event.id,
纬度:event.latitude,
经度:event.longitude,
地址:event.address,
);
如果(更新){
最终传感器更新传感器=等待传感器respository.findSensorById(
id:event.id,
);
产量传感器更新(更新传感器:更新传感器);
等待订阅?取消();
_订阅=sensorsRepository.sensors.listen(
(QuerySnapshot\u快照)=>添加(
_传感器已加载(_snapshot.sensorsFromSnapshot),
),
);
}
}捕获(错误){
产量传感器错误(消息:error.toString());
}
}
如果(事件为DeleteSensor){
屈服传感器加载();
试一试{
最终删除=等待传感器还原。删除传感器(
sensorDbId:event.sensorToBeDeleted.dbId);
如果(已删除){
产量传感器删除();
等待订阅?取消();
_订阅=sensorsRepository.sensors.listen(
(QuerySnapshot\u快照)=>添加(
_传感器已加载(\u s
imports ...
part 'sensors_event.dart';
part 'sensors_state.dart';
class SensorsBloc extends Bloc<SensorsEvent, SensorsState> {
final SensorsRepository sensorsRepository;
StreamSubscription _subscription;
SensorsBloc({
@required this.sensorsRepository,
}) : super(SensorsLoading());
@override
Stream<SensorsState> mapEventToState(
SensorsEvent event,
) async* {
if (event is SensorsRequested) {
yield SensorsLoading();
try {
await _subscription?.cancel();
_subscription = sensorsRepository.sensors.listen(
(QuerySnapshot _snapshot) => add(
_SensorsLoaded(_snapshot.sensorsFromSnapshot),
),
);
} catch (error) {
yield SensorsError(message: error.toString());
}
}
if (event is _SensorsLoaded) {
yield SensorsLoaded(sensors: event.sensors);
}
if (event is SensorsMapRequested) {
yield SensorsLoading();
try {
await _subscription?.cancel();
_subscription = sensorsRepository.sensors.listen(
(QuerySnapshot _snapshot) => add(
_SensorsMapLoaded(_snapshot.sensorsFromSnapshot),
),
);
} catch (error) {
yield SensorsError(message: error.toString());
}
}
if (event is _SensorsMapLoaded) {
yield SensorsMapLoaded(sensors: event.sensors);
}
if (event is AddSensorRequested) {
yield SensorsLoading();
try {
yield AddSensorInitial();
} catch (error) {
yield SensorsError(message: error.toString());
}
}
if (event is AddSensor) {
yield SensorsLoading();
try {
final added = await sensorsRepository.addSensor(
id: event.id,
latitude: event.latitude,
longitude: event.longitude,
address: event.address,
);
if (added) {
final Sensor addedSensor = await sensorsRepository.findSensorById(
id: event.id,
);
yield SensorAdded(addedSensor: addedSensor);
await _subscription?.cancel();
_subscription = sensorsRepository.sensors.listen(
(QuerySnapshot _snapshot) => add(
_SensorsLoaded(_snapshot.sensorsFromSnapshot),
),
);
}
} catch (error) {
yield SensorsError(message: error.toString());
}
}
if (event is UpdateSensorRequested) {
yield SensorsLoading();
try {
yield UpdateSensorInitial(
sensorToBeUpdated: event.sensorToBeUpdated,
isMap: event.isMap,
);
} catch (error) {
yield SensorsError(message: error.toString());
}
}
if (event is UpdateSensor) {
yield SensorsLoading();
try {
final updated = await sensorsRepository.updateSensor(
oldSensor: event.oldSensor,
id: event.id,
latitude: event.latitude,
longitude: event.longitude,
address: event.address,
);
if (updated) {
final Sensor updatedSensor = await sensorsRepository.findSensorById(
id: event.id,
);
yield SensorUpdated(updatedSensor: updatedSensor);
await _subscription?.cancel();
_subscription = sensorsRepository.sensors.listen(
(QuerySnapshot _snapshot) => add(
_SensorsLoaded(_snapshot.sensorsFromSnapshot),
),
);
}
} catch (error) {
yield SensorsError(message: error.toString());
}
}
if (event is DeleteSensor) {
yield SensorsLoading();
try {
final deleted = await sensorsRepository.deleteSensor(
sensorDbId: event.sensorToBeDeleted.dbId);
if (deleted) {
yield SensorDeleted();
await _subscription?.cancel();
_subscription = sensorsRepository.sensors.listen(
(QuerySnapshot _snapshot) => add(
_SensorsLoaded(_snapshot.sensorsFromSnapshot),
),
);
}
} catch (error) {
yield SensorsError(message: error.toString());
}
}
}
@override
Future<void> close() {
_subscription?.cancel();
return super.close();
}
}
class HistoryScreen extends StatefulWidget {
final int _userRights;
const HistoryScreen({
Key key,
@required int userRights,
}) : this._userRights = userRights,
super(key: key);
@override
_HistoryScreenState createState() => _HistoryScreenState();
}
class _HistoryScreenState extends State<HistoryScreen> {
HistoricalEventsBloc _historicalEventsBloc;
@override
void initState() {
super.initState();
_historicalEventsBloc = BlocProvider.of<HistoricalEventsBloc>(context);
}
@override
Widget build(BuildContext context) {
return BlocConsumer<HistoricalEventsBloc, HistoricalEventsState>(
builder: (BuildContext context, HistoricalEventsState state) {
if (state is HistoricalEventsLoaded) {
if (state.events.isNotEmpty) {
return Container(
height: MediaQuery.of(context).size.height,
child: Stack(
children: [
SingleChildScrollView(
child: HistoryList(historicalEvents: state.events),
),
],
),
);
}
return Center(
child: Text('No event in history'),
);
}
return Center(child: CustomCircularIndicator());
},
listener: (BuildContext context, HistoricalEventsState state) {
if (state is HistoricalEventsError) {
showDialog(
context: context,
builder: (context) => CustomPlatformAlertDialog(
title: S.current.register_error_default,
message: Text(
state.message,
style: Styles.defaultGreyRegular14,
),
),
);
}
if (state is HistoricalEventsDeleted) {
showDialog(
context: context,
builder: (context) => CustomPlatformAlertDialog(
title: 'Success',
message: Text('Event deleted'),
),
);
}
if (state is HistoricalEventDetail) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BlocProvider.value(
child: HistoryDetailScreen(
canDelete: widget._userRights == 1,
event: state.event,
sensors: state.sensors,
),
value: _historicalEventsBloc,
),
),
);
}
},
);
}
}
class SensorsScreen extends StatefulWidget {
final int _userRights;
final Function _setMap;
const SensorsScreen({
Key key,
@required int userRights,
@required Function setMap,
}) : this._userRights = userRights,
this._setMap = setMap,
super(key: key);
@override
_SensorsScreenState createState() => _SensorsScreenState();
}
class _SensorsScreenState extends State<SensorsScreen> {
SensorsBloc _sensorsBloc;
@override
void initState() {
super.initState();
_sensorsBloc = BlocProvider.of<SensorsBloc>(context);
}
@override
Widget build(BuildContext context) {
return BlocConsumer<SensorsBloc, SensorsState>(
builder: (BuildContext context, SensorsState state) {
if (state is SensorsLoading ||
state is AddSensorInitial ||
state is UpdateSensorInitial) {
return Center(child: CustomCircularIndicator());
}
if (state is SensorsLoaded) {
if (state.sensors.isNotEmpty) {
return Container(
height: MediaQuery.of(context).size.height,
child: Stack(
children: [
SingleChildScrollView(
child: SensorsList(sensors: state.sensors),
),
Positioned(
left: 16,
bottom: 16,
child: CustomFloatingButton(
onPressed: () {
widget._setMap();
_sensorsBloc.add(SensorsMapRequested());
},
icon: Icon(
Icons.map_outlined,
color: ColorHelper.white,
),
label: S.current.show_on_map,
),
),
],
),
);
}
return Center(
child: Text('No sensor'),
);
}
if (state is SensorsMapLoaded) {
if (state.sensors.isNotEmpty) {
List<Marker> markers = state.sensors
.map(
(Sensor sensor) => Marker(
width: 25.0,
height: 25.0,
point: LatLng(sensor.latitude, sensor.longitude),
builder: (ctx) => Container(
decoration: BoxDecoration(
color: ColorHelper.white,
borderRadius: BorderRadius.circular(30.0),
border: Border.all(color: ColorHelper.darkBlue),
),
child: GestureDetector(
onTap: () => _sensorsBloc.add(
UpdateSensorRequested(
sensorToBeUpdated: sensor,
isMap: true,
),
),
child: Container(
padding: const EdgeInsets.only(top: 4.0),
child: Text(
sensor.id.toString(),
textAlign: TextAlign.center,
style: Styles.darkBlueRegular12,
),
),
),
),
),
)
.toList();
return Stack(
children: [
MapWidget(markers: markers),
Positioned(
left: 16,
bottom: 16,
child: CustomFloatingButton(
onPressed: () {
widget._setMap();
_sensorsBloc.add(SensorsRequested());
},
icon: Icon(
Icons.list_outlined,
color: ColorHelper.white,
),
label: S.current.show_list,
),
),
],
);
}
return Center(
child: Text('No sensor'),
);
}
return Column(
children: [
Text(state.toString()),
Center(
child: RaisedButton.icon(
onPressed: () => _sensorsBloc.add(SensorsRequested()),
icon: Icon(Icons.refresh_outlined),
label: Text('Refresh'),
),
),
],
);
},
listener: (BuildContext context, SensorsState state) {
if (state is SensorsError) {
showDialog(
context: context,
builder: (context) => CustomPlatformAlertDialog(
title: S.current.register_error_default,
message: Text(
state.message,
style: Styles.defaultGreyRegular14,
),
),
);
}
if (state is AddSensorInitial) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: _sensorsBloc,
child: AddEditScreen(
isEdit: false,
canDelete: widget._userRights == 1,
),
),
),
);
}
if (state is UpdateSensorInitial) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BlocProvider.value(
value: _sensorsBloc,
child: AddEditScreen(
isEdit: true,
canDelete: widget._userRights == 1,
sensor: state.sensorToBeUpdated,
isMap: state.isMap,
),
),
),
);
}
if (state is SensorAdded) {
Navigator.pop(context);
showDialog(
context: context,
builder: (context) => CustomPlatformAlertDialog(
title: 'Success',
message: Text(
'Sensor id: ${state.addedSensor.id}\nlatitude: ${state.addedSensor.latitude}\nlongitude: ${state.addedSensor.longitude}',
),
),
);
}
if (state is SensorUpdated) {
Navigator.pop(context);
showDialog(
context: context,
builder: (context) => CustomPlatformAlertDialog(
title: 'Success',
message: Text(
'Sensor id: ${state.updatedSensor.id}\nlatitude: ${state.updatedSensor.latitude}\nlongitude: ${state.updatedSensor.longitude}',
),
),
);
}
if (state is SensorDeleted) {
showDialog(
context: context,
builder: (context) => CustomPlatformAlertDialog(
title: 'Success',
message: Text('Sensor deleted'),
),
);
}
},
);
}
}