我们如何检查consumer
通过接收的数据provider
是否更新或更改,我想在传递给之前添加一个缓冲区来检查Lat Lang
值google_maps_flutter
,我想在传递给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()),
);
}
}),
使用的提供者是更改通知提供者,默认构造函数调用推送器来获取值,调用设置器和获取器函数来检索值。
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();
}
}