如何在SQLite上保存列表数据?,没有列表数据,我知道如何存储。但列出数据我不知道该怎么做。 API附带了1000多个数据。我需要一次存储所有数据的缓存。
另一个问题,存储数据后,我需要每1小时检查一次后端,如果服务器中有新数据,则需要更新本地数据。怎么做?
这是我的模型课,
class AssetRegister {
final List<Data> data;
AssetRegister({this.data});
factory AssetRegister.fromJson(Map<String, dynamic> json) {
var list = json['data'] as List;
print(list.runtimeType);
List<Data> assetList = list.map((i) => Data.fromJson(i)).toList();
return AssetRegister(data: assetList);
}
Map<String, dynamic> toMap() {
return {
'data': data,
};
}
}
class Data {
final int i;
final String d;
final int c;
final int u;
final double p;
final int a;
Data({this.i, this.d, this.c, this.u, this.p, this.a});
factory Data.fromJson(Map<String, dynamic> json) {
return Data(
i: json['i'],
d: json['d'],
c: json['c'],
u: json['u'],
p: json['p'],
a: json['a'],
);
}
}
我尝试过,但是没有用。错误消息是“数据实例”。这是我的代码。
class HelperRegister {
static final HelperRegister _instance = HelperRegister.internal();
factory HelperRegister() => _instance;
static Database _db;
Future<Database> get db1 async {
if (_db != null) return _db;
_db = await initDb();
return _db;
}
HelperRegister.internal();
initDb() async {
io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, "RegisterDB.db");
var theDb = await openDatabase(path, version: 1, onCreate: _onCreate);
return theDb;
}
void _onCreate(Database db, int version) async {
// When creating the db, create the table
await db.execute(
"CREATE TABLE registerTable(i INTEGER PRIMARY KEY, d TEXT, c INTEGER, u INTEGER, p DOUBLE, a INTEGER)");
}
databaseTokenTesting(String tokenModel, String refreshTokenModel,
String method) async {
final database = openDatabase(
join(await getDatabasesPath(), 'RegisterDB.db'),
onCreate: (db, version) {
return db.execute(
"CREATE TABLE registerTable(i INTEGER PRIMARY KEY, d TEXT, c INTEGER, u INTEGER, p DOUBLE, a INTEGER)",
);
},
version: 1,
);
}
Future<void> insert(AssetRegister assetregister) async {
var db = await db1;
await db.insert(
'registerTable',
assetregister.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Future<List<AssetRegister>> display() async {
var db = await db1;
final List<Map<String, dynamic>> maps = await db.query('registerTable');
return List.generate(maps.length, (i) {
return AssetRegister(
data: maps[i]['data'],
);
});
}
Future<void> update(AssetRegister assetregister) async {
var db = await db1;
await db.update(
'tokenTable',
assetregister.toMap(),
where: "i = ?",
whereArgs: [assetregister.data[1].i],
);
}
}
这是我的功能。
storeRegister(String url,String token) async {
final response = await http.get(
'${url}/v1.0/Register',
headers: {'Authorization': 'Bearer ${token}'},);
final jsonResponse = json.decode(response.body);
AssetRegister model = AssetRegister.fromJson(jsonResponse);
await HelperRegister().insert(model);
}
这是我的json
{
"data": [
{
"i": 1,
"d": "Guest Rooms",
"c": 1,
"u": 1,
"p": 1.1,
"a": null
},
{
"i": 5,
"d": "Front of House",
"c": 1,
"u": 1,
"p": 1.2,
"a": null
},
...
]
}