2014-02-05 24 views
1

我正在創建一個函數來存儲IndexedDB中的數據,並保留一個Object,該Object包含一個我分配鍵的Map。使用lawndart獲取indexedDB中對象的值

配鑰匙,我得到的對象的值,但它是一個LinkedHashMap而不是Map,當我嘗試訪問該對象中的數據,我總是得到作爲響應。我已經閱讀了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

如果你只是使用「地圖」你得到一個鏈接哈希映射實現作爲默認值。

該代碼: -

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

應該是你最後一次「然後」語句中,您的訪問數據庫的存儲情況以前發生過。

+0

謝謝!但我打算做的是從indexedDb獲取數據並將其分配給一個對象以便能夠操作它。對不起,如果我的問題不清楚,並再次感謝您的時間。 – Merqurio

+0

我認爲答案仍然準確。你可以看看這個文檔https://www.dartlang.org/docs/tutorials/futures/。 getTheData()在**'nuke'和2個成功的'save'方法之前被執行**。 –

+0

感謝您的快速回答!如果發生這種情況,那麼爲什麼我能夠打印由鍵獲得的值,而不是將其分配給變量?我對未來有點困惑 – Merqurio