如何让flutter cubit倾听背景的变化



所以我想知道如何制作扑动立方体块在flutter的后台工作?

我试着用这个代码来监听变化并发出新的位置——它只在forround 中有效

location.onLocationChanged.listen((LocationData currentLocation) async {
emit(currentLocation);
});

即使我设置了enablebackground,cubit也会停止工作

location.enableBackgroundMode(enable: true);

有更好的方法来实现它吗?

这就是我最终要做的,它运行得很好,只是将您的逻辑从blocbuilder和bloclistenr 中移开

位置状态

// ignore_for_file: public_member_api_docs, sort_constructors_first
part of 'location_cubit.dart';
abstract class LocationState extends Equatable {
@override
List<Object> get props => [];
}
class LocationInitial extends LocationState {
final double lat;
final double lng;
LocationInitial({
required this.lat,
required this.lng,
});
@override
List<Object> get props => [lat, lng];
}
class Locationloading extends LocationState {
Locationloading();
}

定位立方体

import 'dart:async';
// ignore: depend_on_referenced_packages
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:hive/hive.dart';
import 'package:location/location.dart';
part 'location_state.dart';
class LocationCubit extends Cubit<LocationState> {
late StreamSubscription<LocationData> control;
Location elocation = Location();
LocationCubit() : super(LocationInitial(lat: 0, lng: 0)) {
print("Init");
elocation.changeSettings(interval: 8000, distanceFilter: 20);
control = elocation.onLocationChanged
.listen((LocationData location) => {inc(location)});
}
void inc(LocationData location) {
/* This will get called on background and forground too
, BUT UI get updated only in Forground 
( so all BlocBuilder / BlocListner will not be invoked in background
all your logic should be here ( like call http or something)
and when your app is back to forground it's will work ");
*/
print("called on backgroud and forground");
String nice = Hive.box('myBox').get('name');
print(nice);
emit(LocationInitial(
lat: location.latitude!.toDouble(),
lng: location.longitude!.toDouble()));
}
void pause() async {
print("should be paused");
emit(Locationloading());
await elocation.enableBackgroundMode(enable: false);
print("should be ok paused");
control.pause();
emit(LocationInitial(lat: 0, lng: 0));
}
void cont() async {
emit(Locationloading());
print("should be cont");
await elocation.enableBackgroundMode(enable: true);
print("should be ok cont");
LocationData loca = await elocation.getLocation();
control.resume();
(LocationInitial(
lat: loca.latitude!.toDouble(), lng: loca.longitude!.toDouble()));
}
@override
Future<void> close() async {
print("should be called");
await elocation.enableBackgroundMode(enable: false);
control.cancel();
print("should be bye");
return super.close();
}
}

最新更新