使用lawndart获取indexedDB中对象的值

时间:2014-02-05 22:23:05

标签: dart indexeddb dart-html

我正在创建一个函数来存储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
  }

1 个答案:

答案 0 :(得分:1)

如果你只是使用'Map',你会得到一个链接的哈希映射实现作为默认值。

此代码: -

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

应该在你的上一个'then'语句中,你在存储发生之前访问数据库。