每次请求后都缓存当前地址|颤动

问题描述 投票:0回答:1

我能够使用geolocator获取当前位置,但是我想在geolocator中不使用lastKnownLocation来缓存和恢复字符串地址:

这是我想做的,如果gps是:

on = get current address and cache the new address

off = restore from cache

代码:

return Container(
  child: Column(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: <Widget>[
      child: ListTile(
        title: _currentPosition != null? Text(_currentAddress) : Text(**RESTORE_CACHE_HERE**), <-- //I want to restore address here
      ),
      IconButton(
        icon: Icon(Icons.refresh),
        onPressed: (){
          _getCurrentLocation();
        },
      ),
    ]
  )
)
              

_getCurrentLocation() {
    geolocator
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
        .then((Position position) {
      setState(() {
        _currentPosition = position;
      });

      _getAddressFromLatLng();
    }).catchError((e) {
      print(e);
    });
  }

  _getAddressFromLatLng() async {
    try {
      List<Placemark> p = await geolocator.placemarkFromCoordinates(
          _currentPosition.latitude, _currentPosition.longitude);

      Placemark place = p[0];

      setState(() {
        _currentAddress = "${place.country}";
      });
    } catch (e) {
      print(e);
    }
  }

谢谢您的时间

flutter caching path geolocation sharedpreferences
1个回答
0
投票
如果我对您的理解正确,那么您要完成的操作是存储上次捕获的地址,如果没有激活GPS,则将其检索出来。为此,您可以使用SharedPreferencesSQLite,只需查看有关如何使用它们的文档即可。
© www.soinside.com 2019 - 2024. All rights reserved.