0

I've been working with Database connection in Flutter using Sqflite package. Right now I'm facing issue with mapping a list from a Future Function as return value to my Question controller.

While trying to call the function into map, this error occurs

 List<Question> _questions = DbConn().getQues()
      .map(
        (question) =>
        Question(
            id: question['id'],
            question: question['question'],
            options: question['options'],
            answer_index: question['answer_index']),
  ).toList();

This is the where error occurs,

  Future<List<Map>> getQues() async {
    String databasesPath = await getDatabasesPath();

    Directory documentsDirectory = await getApplicationDocumentsDirectory();

    // Directory documentsDirectory = (await getDatabasesPath()) as Directory;
    print(documentsDirectory.uri);
    String path = join(documentsDirectory.path, "Question.db");
    debugPrint('pathDocumentDirectory: $path');
    debugPrint('pathDatabasePath: $databasesPath');


    _database = await openDatabase(
      join(databasesPath, 'Question.db'),
      onCreate: (db, version) {
        debugPrint('called getQues function');
      },
      version: 1,
    );
    final Database db = _database;
    // List<Map> list = await db.execute("SELECT * FROM ques",);
    debugPrint('databaseGetQues: $database');


    var onlyQues = DbConn.sample_data[0]['question'];
    debugPrint("Latest onlyQues: $onlyQues");


    for (int i = 0; i < DbConn.sample_data.length; i++) {
      var onlyQuesFor = DbConn.sample_data[i]['question'];
      debugPrint("Latest onlyQuesFor: $onlyQuesFor");
    // }

    var exId = DbConn.sample_data[i]['id'];
    var exQuestion = DbConn.sample_data[i]['question'];
    var exOptions = DbConn.sample_data[i]['options'];
    var exAnswer_index = DbConn.sample_data[i]['answer_index'];

    int insertId1 = await _database.rawInsert(
        'INSERT INTO quesTable(id, question, options, answer_index) VALUES($exId, "$exQuestion", "$exOptions", $exAnswer_index)');
    debugPrint('id1: $insertId1');
  }
    List<Map> list = await db.rawQuery('SELECT * FROM quesTable');

    debugPrint('List: $list');
    return list;
  }
Terin Tittu
  • 143
  • 1
  • 1
  • 15
  • Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Oct 05 '21 at 14:12

1 Answers1

2

To access the value of a future, you have to await it, instead of directly assigning the future to a variable lets use a function:

getListOfQuestions() async {
  var _response = await DbConn().getQues();
  List<Question> _questions = _response
      .map(
        (question) => Question(
            id: question['id'],
            question: question['question'],
            options: question['options'],
            answer_index: question['answer_index']),
      )
      .toList();
  return _questions;
}
Hooshyar
  • 1,181
  • 4
  • 12
  • 28
  • Thanks for this answer. I tried using this function but couldn't use the value that's inside Future. How can I access _questions outside the Future? – Terin Tittu Oct 07 '21 at 10:43
  • 1
    Check ‘return _questions’, you know that to access a future value in UI you should use ‘FutureBuilder’, correct? – Hooshyar Oct 07 '21 at 11:19
  • I did try returning _question, the same repeats. I also checked using FutureBuilder but the thing is I'm using this value and assigning it Globally outside a function so that I can map this to another class(Body()). – Terin Tittu Oct 07 '21 at 11:41
  • I just need this below list to use globally, List list = await db.rawQuery('SELECT * FROM quesTable'); Since we cannot use await other than Future it doesn't get mapped properly when assigned to list. Can you please help me to find a solution – Terin Tittu Oct 07 '21 at 12:23
  • Sure, Let me test your project using a real Sqflite database, just please remove the images from your question and put the code itself. – Hooshyar Oct 07 '21 at 13:50