flutter 如何知道我正在进入哪个地理围栏?

zzoitvuj  于 2023-01-21  发布在  Flutter
关注(0)|答案(1)|浏览(142)

我创建了一个地理围栏列表,我需要地理围栏ID将其链接到下一页。如何获得我输入的地理围栏的索引号?

flutter package : geofence_service: ^3.5.0
final _geofenceList = <Geofence>[]; //assume there are 3 geofence in that list

Widget? _widget(){
    if(_geofenceStatus == GeofenceStatus.ENTER){
          NotificationApi.showNotification(
              title: 'SideQuest',
              body: 'There is an event near you !!!',
              payload: 'sarah.abs'
          );
          print(_geofenceList[???].id);
    }
  }
6rqinv9w

6rqinv9w1#

您可以使用.indexWhere()方法查找正在输入的地理围栏的索引。您可以使用回调函数检查正在输入的地理围栏是否与列表中的地理围栏具有相同的ID。请看以下示例:

Widget? _widget(){
    if(_geofenceStatus == GeofenceStatus.ENTER){
          NotificationApi.showNotification(
              title: 'SideQuest',
              body: 'There is an event near you !!!',
              payload: 'sarah.abs'
          );
          // Get the index of the geofence that is being entered
          int index = _geofenceList.indexWhere((geofence) => geofence.id == _geofenceStatus.geofenceId);
          print(_geofenceList[index].id);
    }
}

相关问题