1

I am trying to fetch a list of questions from an endpoint that returns json formatted like so:

[
  {
    "id": 1,
    "question": "text",
    "option1": "text",
    "option2": "text",
    "option3": "text",
    "option4": "text",
    "answer": "text"
  },
  {
    "id": 2,
    "question": "text",
    "option1": "text",
    "option2": "text",
    "option3": "text",
    "option4": "text",
    "answer": "text"
  }
]

Following the example here, I have a quiz class:

class Quiz extends Object with _$QuizSerializerMixin {
  List<Question> questions;

  Quiz(this.questions);

  factory Quiz.fromJson(Map<String, dynamic> json) => _$QuizFromJson(json);
}

and question class:

class Question extends Object with _$QuestionSerializerMixin {

  final String question;
  final String option1;
  final String option2;
  final String option3;
  final String option4;
  final String answer;

  Question(
      {this.question,
      this.option1,
      this.option2,
      this.option3,
      this.option4,
      this.answer});

  factory Question.fromJson(Map<String, dynamic> json) =>
      _$QuestionFromJson(json);
}

with the function defined as:

Future<Quiz> fetchQuiz() async {
  final response = await http.get('json_endpoint.placeholder/questions');

  if (response.statusCode == 200) {
    return Quiz.fromJson(json.decode(response.body));
  } else {
    throw Exception('Failed to load quiz');
  }
}

how do I pass this into a new instance of quiz? This may be obvious but I am finding it difficult.

I have tried declaring a variable as follows:

class QuizScreen extends StatefulWidget {
  @override
  _QuizScreenState createState() => _QuizScreenState();
}

class _QuizScreenState extends State<QuizScreen> {
Question question
String option1
...

Quiz quiz = quiz(fetchQuiz());

@override
void initState() {
super.initState();
question = quiz.nextQuestion;
option1 = question.option1;
...

Quiz quiz = Quiz(fetchQuiz()); throws an error.

7
  • "which gives an error." please add the full error output. Commented Jun 21, 2018 at 7:01
  • Its an error from the IDE (Android Studio) "The argument type 'Future<Quiz>' can't be assigned to the parameter type 'List<Question>'" Commented Jun 21, 2018 at 7:11
  • Looks like you are trying to assign the result from fetchQuiz() to List<Question> questions; but I don't see in your question where this might happen. Perhaps that code is missing. Commented Jun 21, 2018 at 7:14
  • Yes, I have updated the question to reflect how I am trying to assign it. Commented Jun 21, 2018 at 7:25
  • The code above doesn't include the change I suggested in my answer. It's also not clear what method contains Quiz quiz = quiz(fetchQuiz()); and also not what quiz(...) is or does. Commented Jun 21, 2018 at 7:29

2 Answers 2

2

The code should look like

Future foo() async {
  Quiz quiz = await fetchQuiz();
} 

fetchQuiz() returns a Future<Quiz> and to get the value out, you use await, for that the function that contains the code (foo() in my example) needs to be made async.

Sign up to request clarification or add additional context in comments.

1 Comment

I am trying to create a new object that would hold the data retrieved, which I then use to populate some Text Widgets created later.
1

I think you're looking for FutureBuilder. It lets you build UI when the Future completes. The result will be in snapshot.data if snapshot.hasData == true.

@override
  Widget build(BuildContext context) {
    return Scaffold(
        body: FutureBuilder<Quiz>(
      future: fetchQuiz(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return _buildQuiz(context, snapshot.data);
        } else if (snapshot.hasError) {
          return Text('${snapshot.error}');
        }
        return Center(child: CircularProgressIndicator());
      },
    ));
  }

void _buildQuiz(BuildContext context, Quiz quiz) {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.