import 'dart:convert' as convert;
|
|
import 'package:locations/model/map/poi_model.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
class SearchUtil {
|
//添加搜索记录
|
static addSearchRecord(PoiModel info) async {
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
List<String>? list = prefs.getStringList("searchRecord");
|
list ??= [];
|
|
for (int i = 0; i < list.length; i++) {
|
PoiModel poiInfo = PoiModel.fromJson(convert.jsonDecode(list[i]));
|
if (poiInfo.address == info.address) {
|
list.removeAt(i);
|
break;
|
}
|
}
|
|
if (list.length > 10) {
|
list.removeRange(10, list.length);
|
}
|
list.add(convert.jsonEncode(info.toJson()));
|
|
prefs.setStringList("searchRecord", list);
|
}
|
|
///获取记录
|
static Future<List<PoiModel>> getRecordList() async {
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
List<String>? list = prefs.getStringList("searchRecord");
|
print(list);
|
list ??= [];
|
List<PoiModel> resultList = [];
|
list.reversed.forEach((element) {
|
PoiModel info = PoiModel.fromJson(convert.jsonDecode(element));
|
resultList.add(info);
|
});
|
return resultList;
|
}
|
}
|