【问题标题】:flutter/ how to save geopoint using shared preferences颤振/如何使用共享偏好保存地理点
【发布时间】:2021-12-31 18:19:33
【问题描述】:

我想知道如何在我的颤振应用中使用共享偏好来保存用户的地理点。它有 2 个值(纬度,经度),但我只能写以下代码,但没有用。

     static String sharedPreferenceUserGeopointKey = "USERGEOPOINTKEY";

     static Future<bool> saveUserGeopointSharedPreference(var geopoint) async {
     SharedPreferences preferences = await SharedPreferences.getInstance();
     return await preferences.setDouble(
     sharedPreferenceUserGeopointKey, geopoint);
     }

后来我像这样检索这些数据;

static Future<dynamic> getUserGeopointSharedPreference() async {
  SharedPreferences preferences = await SharedPreferences.getInstance();
  double mylat = 
preferences.getDouble(sharedPreferenceUserGeopointLatKey);
  double mylong = 
preferences.getDouble(sharedPreferenceUserGeopointLongKey);
  return true;
}

bool result = await getUserGeopointSharedPreferences();
GeoPoint geopoint = result;

我无法将result 放入 GeoPoint 类型的地理点对象中,我可以这样做吗?

【问题讨论】:

    标签: flutter sharedpreferences


    【解决方案1】:

    您需要使用不同的键调用两次 setDouble。

    或者尝试使用共享首选项类的 setStringList() 方法。
    在这种情况下,您需要从双纬度、对数度更改为列表字符串。

         static String sharedPreferenceUserGeopointLatitudeKey = "USERGEOPOINT_LATITUDE_KEY";
         static String sharedPreferenceUserGeopointLogitudeKey = "USERGEOPOINT_LONGITUDE_KEY";
    
         static Future<bool> saveUserGeopointSharedPreference(var lat, var long) async {
            SharedPreferences preferences = await SharedPreferences.getInstance();
            await preferences.setDouble(
               sharedPreferenceUserGeopointLatitudeKey, lat);
            await preferences.setDouble(
               sharedPreferenceUserGeopointLogitudeKey, long);
            return true
         }
    
    static Future<GeoPoint> getUserGeopointSharedPreference() async {
      SharedPreferences preferences = await SharedPreferences.getInstance();
      double mylat = 
    preferences.getDouble(sharedPreferenceUserGeopointLatKey);
      double mylong = 
    preferences.getDouble(sharedPreferenceUserGeopointLongKey);
      return GeoPoint.fromLatLng(name: 'Position', point: LatLng(mylat, mylong);
    }
    
    GeoPoint geopoint = await getUserGeopointSharedPreferences();
    
    

    【讨论】:

    • 感谢您的回答。我试过这个,但我无法检索我保存的地理点。当我将保存的地理点放入地理点类型对象时,出现错误消息type 'bool' is not a subtype of type 'GeoPoint'。怎么了?
    • 您似乎收到了“saveUserGeopointSharedPreference”方法对 GeoPoint 变量的响应。因为 'saveUserGeopointSharedPreference' 方法的响应类型是 'bool' ,所以尝试这样做 'bool result = await saveUserGeopointSharedPreference(....)',
    • 在尝试之后,如何将bool result 保存到 GeoPoint 类型的对象中?我试过GeoPoint geopoint = result;,结果还是一样
    • 'saveUserGeopointSharedPreference' 方法仅用于将地理点值保存到 sharedpreference。当您调用“saveUserGeopointSharedPreference”方法时,您已经有了一个地理点变量,因为您将地理点变量作为参数传递。所以你不需要将结果保存到 Geopoint 变量。
    • 当你调用'loadUser....'方法从sharedpreference geopoint值中获取时,你需要将结果保存到geopoint变量中。在此示例中,没有 'loadUser...' 方法。
    猜你喜欢
    • 2021-01-27
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 2020-02-04
    相关资源
    最近更新 更多