Flutter Provider中如何检查消费者收到的数据何时更新



我们如何检查通过providerconsumer中接收的数据是否更新或更改,我想在传递到google_maps_flutter之前添加一个缓冲区来检查Lat Lang的值,我想传递到google_maps_flutter小部件之前检查该值5次以更新位置

Consumer<PuhserDataProvider>(builder: (context, data, child) {
if (data.devicePusherData != null) {
final lat = extractLat("${data.devicePusherData.gps}");
final lang = extractLang("${data.devicePusherData.gps}");
log.w(lat);
log.w(lang);
return GoogleMap(
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
myLocationButtonEnabled: false,
initialCameraPosition: CameraPosition(
target: LatLng(lat, lang),
zoom: 16,
),
markers: [
Marker(
markerId: MarkerId('0'),
position: LatLng(lat, lang),
onTap: () =>
setState(() => selectedPoint = LatLng(lat, lang)))
].toSet(),
onTap: (point) => setState(() => selectedPoint = null),
);
} else {
return Container(
height: MediaQuery.of(context).size.height * 0.8,
width: double.infinity,
child: Center(child: CircularProgressIndicator()),
);
}
}),

所使用的提供程序是一个更改通知程序提供程序,Default constructer函数调用pusher获取值,调用setter和getter函数检索值。

class PuhserDataProvider extends ChangeNotifier {
final Pusher pusher;
Logger log = getLogger("PuhserDataProvider");
DevicePusherData _devicePusherData;
DevicePusherData get devicePusherData => _devicePusherData;
OBDPuhserData _obdPusherData;
OBDPuhserData get obdPusherData => _obdPusherData;
PuhserDataProvider(String imei, String token, String pusherKey)
: pusher = Pusher(
pusherKey,
PusherOptions(
cluster: 'eu',
authEndpoint: AUTH_URL,
auth: PusherAuth(headers: {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
'Accept': 'application/json'
})),
) {
Channel channel = pusher.subscribe('private-$imei-send');
channel.bind('obd-event',
(data) => _setOBDData(OBDPuhserData.fromJson(json.decode(data)[0])));
channel.bind(
'deviceevent',
(data) =>
_setDeviceData(DevicePusherData.fromJson(json.decode(data)[0])));
}
_setDeviceData(DevicePusherData devicePusherData) {
this._devicePusherData = devicePusherData;
notifyListeners();
}
_setOBDData(OBDPuhserData obdPusherData) {
this._obdPusherData = obdPusherData;
notifyListeners();
}
}

您的类应该看起来像:

PusherDataProvider with ChangeNotifier {
final Object _value;
set value(Object value) {
if (value != _value) {
_value = value;
// Notify listeners only when data changed
notifyListeners();
}
Object get value => _value;
}
}

所以现在,当数据更改时,将调用yorConsumer的生成器。声明一个计数器变量,该变量将在生成器中进行检查。

您可以通过两种方法来实现这一点

  1. 使用Selector((而不是Consumer((
  2. 使用PuhserDataProvider((类

1.使用Selector((而不是Consumer((

相当于Consumer,它可以通过选择有限数量的值来过滤更新,并在更新不更改时阻止重建。


2。使用PuhserDataProvider((类

在调用notifyListers((之前检查您的逻辑

此处为

class PuhserDataProvider extends ChangeNotifier {
final Pusher pusher;
Logger log = getLogger("PuhserDataProvider");

//I changed _devicePusherData into _devicePusherDataGps
//I dont know Data type of your .gps , so temperary i said it as Object , you need to change it
Object _devicePusherDataGps;
//I changed devicePusherData into devicePusherDataGps 
//I dont know Data type of your .gps , so temperary i said it as Object , you need to change it
Object get devicePusherDataGps => _devicePusherDataGps;
//counter variable with default value 1 ,this will increment when each time value changed
int counter = 1;
PuhserDataProvider(String imei, String token, String pusherKey)
: pusher = Pusher(
pusherKey,
PusherOptions(
cluster: 'eu',
authEndpoint: AUTH_URL,
auth: PusherAuth(headers: {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
'Accept': 'application/json'
})),
) {
Channel channel = pusher.subscribe('private-$imei-send');
channel.bind(
'deviceevent',
(data) =>
_setDeviceData(DevicePusherData.fromJson(json.decode(data)[0])));
}
_setDeviceData(DevicePusherData devicePusherData) {
if (_devicePusherDataGps == null) {
_devicePusherDataGps = devicePusherData.gps;
}
else{
if (devicePusherData.gps != _devicePusherDataGps) {
//This will check value changes for 5 times.
if (counter == 5) {
counter = 1;
this._devicePusherDataGps = devicePusherData.gps;
notifyListeners();
} else {
counter++;
}
}
}

}
}

在上述类中,setter函数仅更改为设置gps值。因为在你的问题中,你说你想单独检查Lat-Lang值,所以我为它创建了单独的setter方法。


现在在Consumer小部件中,您需要将data.devicePusherData.gps更改为data.devicePusherDataGps

final lat = extractLat("${data.devicePusherDataGps}");
final lang = extractLang("${data.devicePusherDataGps}");

希望这对你有帮助。如果你对上面有任何问题,请评论,我会尽力帮助你。(有时代码只是有语法错误,因为我没有要导入的json模型类(

最新更新