简体   繁体   中英

I am trying to make a quiz app in flutter with geeks for geeks tutorial as reference.. i have following errors

I followed the whole tutorial word to word, here is the link: https://www.geeksforgeeks.org/basic-quiz-app-in-flutter-api/

I have two errors

one in main:

Result(_totalScore, _resetQuiz(context)) in this line it says:

This expression has a type of 'void' so its value can't be used. Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also, check type parameters and variables which might also be void.

import 'package:flutter/rendering.dart';

import './quiz.dart';
import './result.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  final _questions = const [
    {
      'questionText': '1. ABC?',
      'answers': [
        {'text': 'E', 'score': 0},
        {'text': 'F', 'score': 0},
        {'text': 'D', 'score': 1},
        {'text': 'S', 'score': 0},
      ],
    },
    {
      'questionText': '1. ABC?',
      'answers': [
        {'text': 'E', 'score': 0},
        {'text': 'F', 'score': 0},
        {'text': 'D', 'score': 1},
        {'text': 'S', 'score': 0},
      ],
    },
    {
      'questionText': '1. ABC?',
      'answers': [
        {'text': 'E', 'score': 0},
        {'text': 'F', 'score': 0},
        {'text': 'D', 'score': 1},
        {'text': 'S', 'score': 0},
      ],
    },
    {
      'questionText': '1. ABC?',
      'answers': [
        {'text': 'E', 'score': 0},
        {'text': 'F', 'score': 0},
        {'text': 'D', 'score': 1},
        {'text': 'S', 'score': 0},
      ],
    },
  ];

  var _questionIndex = 0;
  var _totalScore = 0;

  void _resetQuiz(BuildContext context) {
    setState(() {
      _questionIndex = 0;
      _totalScore = 0;
    });
  }

  void _answerQuestion(int score) {
    _totalScore += score;

    setState(() {
      _questionIndex = _questionIndex + 1;
    });
    print(_questionIndex);
    if (_questionIndex < _questions.length) {
      print('We have more questions!');
    } else {
      print('No More Questions :(');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Quiz'),
          backgroundColor: Colors.black,
        ),
        body: Padding(
          padding: const EdgeInsets.all(30.0),
          child: _questionIndex < _questions.length
              ? Quiz(
                  answerQuestion: _answerQuestion,
                  questionIndex: _questionIndex,
                  questions: _questions,
                )
              : Result(_totalScore, _resetQuiz(context)),
        ),
      ),
      debugShowCheckedModeBanner: false,
    );
  }
}

one errors in quiz.dart

in the following line: (questions[questionIndex]['answers']) as List<Map<String, Object>>)

import './answers.dart';
import './questions.dart';

class Quiz extends StatelessWidget {
  final List<Map<String, Object>> questions;
  final int questionIndex;
  final Function answerQuestion;

  Quiz({
    @required this.questions,
    @required this.answerQuestion,
    @required this.questionIndex,
  });

  @override 
  Widget build(BuildContext context){
    return Column(
      children: [
        Question (
          questions[questionIndex]['questionText'],
        ),
        ...(questions[questionIndex]['answers']) as List<Map<String, Object>>)
              .map((answer)
              {
                return Answer(()=> answerQuestion(answer['score']), answer['text']
                );
              }).toList()
      ],
      );
  }
}

errors: The element type 'Map<String, Object>' can't be assigned to the list type 'Widget'.

The first error: you should do it like this Result(_totalScore, _resetQuiz) , you don't have to add a parameter because you are passing a function not to call the function itself.

The second error: it's a typo ...(questions[questionIndex]['answers'] as List<Map<String, Object>>)

For the first error Result method is returning void instead of widget. Check the function again. You might be missing a return statement. Same thing with the second error. Instead of returning widget the method is returning map<string, object>. If that object is widget try accessing it using ["object name"].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM