You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aku_new_community/lib/provider/data_provider.dart

53 lines
1.6 KiB

import 'package:aku_new_community/constants/sars_api.dart';
import 'package:aku_new_community/models/login/china_region_model.dart';
import 'package:aku_new_community/models/login/history_login_model.dart';
import 'package:aku_new_community/utils/hive_store.dart';
import 'package:aku_new_community/utils/network/net_util.dart';
3 years ago
import 'package:aku_new_community/widget/others/user_tool.dart';
import 'package:flutter/material.dart';
class DataProvider extends ChangeNotifier {
Future init() async {
_loginHistories = HiveStore.dataBox!
.get('historyLogin')
?.cast<HistoryLoginModel>()
.toList() ??
[];
3 years ago
_cityModel =
HiveStore.dataBox!.get('cities')?.cast<ChinaRegionModel>().toList() ??
[];
if (_cityModel.isEmpty) {
updateCityList();
}
}
List<ChinaRegionModel> _cityModel = [];
List<ChinaRegionModel> get cityModel => _cityModel;
Future<bool> updateCityList() async {
var model = await NetUtil().get(SARSAPI.city.allCity);
if (model.success) {
_cityModel = (model.data as List)
.map((e) => ChinaRegionModel.fromJson(e))
.toList();
HiveStore.dataBox!.put('cities', _cityModel);
return true;
} else {
return false;
}
}
List<HistoryLoginModel> _loginHistories = [];
List<HistoryLoginModel> get loginHistories => _loginHistories;
3 years ago
Future addHistories() async {
_loginHistories.insert(0, UserTool.appProveider.pickedCityAndCommunity!);
if (_loginHistories.length > 4) {
_loginHistories.removeAt(_loginHistories.length - 1);
}
HiveStore.dataBox!.put('historyLogin', _loginHistories);
}
}