【问题标题】:How can I execute bloc event continuously in Flutter?如何在 Flutter 中连续执行 bloc 事件?
【发布时间】:2020-08-17 09:38:12
【问题描述】:

我想在我的应用程序中持续跟踪用户位置。出于这个原因,我想在我的主页中连续执行 getLocaiton bloc 事件。我该怎么做?

这是我的 getLocation 事件;

  GetLocationState get initialState => GetLocationState(saveUserCurrentLocation: initialPosition );

  Stream<GetLocationState> mapEventToState(LocationEvent event) async*{

    if (event is GetLocation){

      userLocation.getLocation().then((l){
        currentLatLng = LatLng(l.latitude, l.longitude);

      });


      yield GetLocationState(saveUserCurrentLocation: currentLatLng);


    }
  }

这是我的主页

            initialCameraPosition:
                CameraPosition(target: initialPosition, zoom: 10),
            mapType: MapType.terrain,
            onMapCreated: _onMapCreated,
            myLocationEnabled: true,
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: () {
              _locationBloc.add(GetLocation());
            },
          ),
        ),
      );
    });
  }

如何在不使用 onPress 事件的情况下在后台连续运行代码?

【问题讨论】:

    标签: google-maps flutter dart mobile


    【解决方案1】:

    initState() 中使用Timer(如果不是,则使您的小部件有状态):

        Timer(Duration(seconds: 5), (){
          _locationBloc.add(GetLocation());
        });
    

    【讨论】:

      猜你喜欢
      • 2021-06-07
      • 1970-01-01
      • 2022-07-26
      • 2023-02-17
      • 2021-12-07
      • 2019-12-06
      • 2021-12-05
      • 2019-11-15
      • 2022-01-01
      相关资源
      最近更新 更多