shyamantha
shyamantha

Reputation: 77

How to get values from Future<List> in Flutter

I want to get value from sqflite database to a list in flutter. Here is my model class


class Task {
  final int id;
  final String title;
  final String description;
  final String dateTime;

  Task({this.id, this.title, this.description, this.dateTime});

  
  Map<String, dynamic> toMap() {
    return {
      'id':id,
      'title': title,
      'description': description,
      'dateTime': dateTime,
    };
  }

This is databaseHelper class for insert and retrieve data.


class DatabaseHelper {
  DatabaseHelper._();
  static final DatabaseHelper db = DatabaseHelper._();

  Database _database;

  Future<Database> get database async {
    if (_database != null) return _database;
    _database = await _createDatabase();
    return _database;
  }

  Future<Database> _createDatabase() async {
    return await openDatabase(join(await getDatabasesPath(), 'task.db'),
        onCreate: (db, version) {
      return db.execute(
        "CREATE TABLE tasks(id INTEGER PRIMARY KEY AUTO_INCREMENT , title TEXT, description TEXT, dateTime TEXT)",
      );
    }, version: 1);
  }


Future<List<Task>> tasks() async {
 
  final Database db = await database;

  final maps = await db.query('tasks');

  return List.generate(maps.length, (i) {
    return Task(
      id: maps[i]['id'],
      title: maps[i]['title'],
      description: maps[i]['description'],
      dateTime: maps[i]['dateTime']
    );
  });
}



Future<void> insertTask(Task task) async {
  final db = await database;


  await db.insert(
    'tasks',
    task.toMap(),
    conflictAlgorithm: ConflictAlgorithm.replace,
  );
}
}

When I use print(await tasks()) it shows [Instance of 'Task', Instance of 'Task'] in console. is it a bug?. I want to know how to get values form database using task() function in DatabaseHelper class.

Upvotes: 0

Views: 1766

Answers (2)

Alberto Miola
Alberto Miola

Reputation: 4751

When I use print(await tasks()) it shows [Instance of 'Task', Instance of 'Task'] in console. is it a bug?

It happens because you haven't overridden toString(). Without toString() you just get the default string conversion "Instance of XXX":

class Example {}

void main() async {
  print(Example());  // Instance of 'Example'
}

By overriding toString() you can customize the output:

class Example {
  @override
  String toString() => "My custom Example!";
}

void main() async {
  print(Task());  // My custom Example!
}

I want to know how to get values form database using task()

The most common way in Flutter to await for a Future<T> is using the FutureBuilder<T> widget. It's very convenient because it can display a progress indicator (to let the user know that something is going on in the background) that will be replaced later by the actual data

Upvotes: 1

Jay Dangar
Jay Dangar

Reputation: 3469

use future builder, which requires future and will build whenever future's object changes.

Upvotes: 1

Related Questions