flutter SharedPreferences 存储对象

存储定义的logisticsDetalEntity对象
_requestLogisticDetal(BuildContext context) async {
  var path = "http://v.senderPhone=&receiverPhone=&dtype=&key=be0d84e149f047b1fc32b50b50d9fc7f";
  var params = {"com": Provider.of(context).logisticsId, "no": "YT9073133846767"};
  Response response = await Dio().post(path, queryParameters: params);
  LogisticsDetalEntity logisticsDetalEntity = LogisticsDetalEntity.fromJson(json.decode(response.toString()));
  if (logisticsDetalEntity.resultcode == "200") {
    setdata(logisticsDetalEntity);//存储
    Provider.of(context).setLogisticsDetalEntity(logisticsDetalEntity);
  } else {
    ToastOk.show(msg: logisticsDetalEntity.reason);
  }
}

void setdata(LogisticsDetalEntity logisticsDetalEntity) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setString("logisticsDetalEntity", jsonEncode(logisticsDetalEntity).toString());
}

取出定义的logisticsDetalEntity对象数据

getdata() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  var data = prefs.getString("logisticsDetalEntity");
  if (data != null) {
    ///因为取出来的json,这里直接用进行映射成对应的map形式,下面进行class的赋值操作。
    Map responseJson = json.decode(data);
    LogisticsDetalEntity logisticsDetalEntity = new LogisticsDetalEntity.fromJson(responseJson);
    ToastOk.show(msg: "本地数据处理成功:" + logisticsDetalEntity.result.company);
  }
}

 

你可能感兴趣的:(flutter使用记录)