Get the values of the object in the indexedDB with lawndart

Issue

I’m creating a function to store data in IndexedDB, keeping an Object that consists of a Map to which I assign a key.

With the key, I get the value of the object, but it’s a LinkedHashMap and not a Map and when I try to access the data inside the object, I always get null as response. I’ve read the API docs, but I’m new on this and I don’t see how to make it work.

What I intend to do is to get the data from the indexedDb and assign it to an object to be able to manipulate it.

Thanks a lot in advance !!!

  /**
   *  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
  }

Solution

If you just use ‘Map’ you get a linked hash map implementation as a default.

This code :-

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

should be inside your last ‘then’ statement, your accessing the db before the storage has happened.

Answered By – user2685314

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *