gpt4 book ai didi

dart - 使用lawndart获取indexedDB中对象的值

转载 作者:行者123 更新时间:2023-12-03 03:47:09 26 4
gpt4 key购买 nike

我正在创建一个函数来将数据存储在IndexedDB中,保留一个由要为其分配键的Map组成的Object。

使用键,我得到对象的值,但这是一个LinkedHashMap而不是Map,当我尝试访问对象内部的数据时,总是得到null作为响应。我已经阅读了API文档,但是对此我还是陌生的,而且我不知道如何使其工作。

我打算做的是从indexedDb获取数据并将其分配给一个对象,以便能够对其进行操作。

提前非常感谢!

  /**
* Opens the database, erases it and saves some info
*/
void openDB(var db) {
Map ourList = {'title':'A random title',
'text':'Vivamus sit amet libero turpis, non venenatis urna.',
'data':'2013'};
Map ourList2 = {
'title':'Another random title',
'text':'Vivamus sit amet libero turpis, non venenatis urna.',
'data':'2014'};
db.open()
.then((_) => db.nuke()) /// Erases all the data in the DB
.then((_) => db.save(ourList, '1')) /// Saves an object in the DB
.then((_) => db.save(ourList2, '2'));
print('openDB() was executed'); /// Console control
getTheData(db);
}

/**
* Gets the data from the DB JUST AN EXAMPLE !!
*/

void getTheData(var db){
Map dbData; /// dbData = null for definition
db.open() /// lawndart method
.then((_) => db.getByKey('1')) /// Gets a value depending on the Key
.then((value) => print(value)); /// Prints the value of the key
db.open()
.then((_) => db.getByKey('2'))
.then((value) => print(value)); /// To check != values, use different methods
db.open()
.then((_) => db.getByKey('2'))
.then((value) => print(value.runtimeType)); /// prints the Type of var

/**
* What type of variable it is: _LinkedHashMap
* Classes implement the Map interface and offers mostly the same functionality.
* LinkedHashMap will iterate in the order in which the entries were put into the map
*/
print('getTheData() was executed');/// Console control
}

最佳答案

如果仅使用“ map ”,则默认情况下会获得链接的哈希 map 实现。

此代码:-

print('openDB() was executed'); /// Console control
getTheData(db);

应该在您的最后一个“then”语句中,即存储发生之前您访问数据库的时间。

关于dart - 使用lawndart获取indexedDB中对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21589860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com