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

问题描述

我们如何检查通过 consumerprovider 中接收的数据是否更新或更改,我想在传递给 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,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()),);
          }
        }),

所使用的提供程序是更改通知程序提供程序,默认构造函数调用推送器获取值,调用 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();
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)