Sameera Godakanda
Sameera Godakanda

Reputation: 109

Using async/wait in dart not waiting with http package

I've read the previous questions posted related to this topic but couldn't understand whats going on. I think I've done exactly the same process explained in this official dart/flutter video - https://www.youtube.com/watch?v=SmTCmDMi4BY

What I'm trying to do is very simple. I want to make an API call and need to wait until I get a response. but I can't see to get it working. Maybe my understanding on async/wait is wrong. If I check the console output I get like this

Staring the Program
Ending the Program
Got the questions list
[Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question']

Can someone help me with this.

import 'package:http/http.dart' as http;
import 'dart:convert';

void main() {
  print('Staring the Program');
  getQuestions();
  print('Ending the Program');
}

void getQuestions() async {
  QuizModel qm = QuizModel();
  List<Question> questionsList = await qm.getQuestions();
  print('Got the questions list');
  print(questionsList);
}

class Question {
  String question;
  bool answer;
  Question({this.question, this.answer});
}

class QuizModel {
  var quizData;

  Future<List<Question>> getQuestions() async {
    NetworkHelper networkHelper =
        NetworkHelper('https://opentdb.com/api.php?amount=10&type=boolean');
    quizData = await networkHelper.getData();
    final List<Question> questionsList = [];
    for (final question in quizData['results']) {
      bool answer;
      question['correct_answer'] == 'True' ? answer = true : answer = false;
      questionsList
          .add(Question(question: question['question'], answer: answer));
    }
    return questionsList;
  }
}

class NetworkHelper {
  final String _url;
  NetworkHelper(this._url);

  Future<dynamic> getData() async {
    http.Response response = await http.get(_url);

    if (response.statusCode == 200) {
      var jsonData = jsonDecode(response.body);
      return jsonData;
    } else {
      print(response.statusCode);
    }
  }
}

Upvotes: 0

Views: 1194

Answers (3)

Rob C
Rob C

Reputation: 2441

QuizModel.getQuestions() is a future.

Your program is printing starting, calling a future, and printing ending. It does not wait for the future, getQuestions.

You can add async to your main, and await on the method call.

Upvotes: 2

Gonzalo Gauto
Gonzalo Gauto

Reputation: 181

I/flutter (18222): Staring the Program
I/flutter (18222): Got the questions list
I/flutter (18222): [Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question', Instance of 'Question']
I/flutter (18222): Ending the Program

if you need to get this print u must replace this

void main() {
  print('Staring the Program');
  getQuestions();
  print('Ending the Program');
}

void getQuestions() async {
  QuizModel qm = QuizModel();
  List<Question> questionsList = await qm.getQuestions();
  print('Got the questions list');
  print(questionsList);
}

with this

void main()async {
  print('Staring the Program');
  await getQuestions();
  print('Ending the Program');
}

Future<void> getQuestions() async {
  QuizModel qm = QuizModel();
  List<Question> questionsList = await qm.getQuestions();
  print('Got the questions list');
  print(questionsList);
}

Upvotes: 0

Gonzalo Gauto
Gonzalo Gauto

Reputation: 181

you're printing the a questionlist instance. if you want to print the questions items you need to do this

void getQuestions() async {
  QuizModel qm = QuizModel();
  List<Question> questionsList = await qm.getQuestions();
  print('Got the questions list');
  for(final question in questionsList){
    print(question.question);
  }
}

instead of

void getQuestions() async {
  QuizModel qm = QuizModel();
  List<Question> questionsList = await qm.getQuestions();
  print('Got the questions list');
  print(questionsList);
}

Upvotes: 0

Related Questions