Home > Software engineering >  3 positional argument(s) expected, but 0 found. 1
3 positional argument(s) expected, but 0 found. 1

Time:08-05

import 'package:flutter/material.dart';
import './question.dart';
import './answer.dart';
import './quiz.dart';
import './result.dart';

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

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

class _MyAppState extends State<MyApp> {
//class MyAppState extends State{
  final _questions = const [
    {
      'questionText': "What\'s your favourite color?",
      'answer': ['Black', 'Red', 'Green', 'White'],
    },
    {
      'questionText': 'What\'s your fav animal?',
      'answer': ['Wolf', 'Cat', 'Dog', 'Tiger'],
    },
    {
      'questionText': 'Who is your fav instructor?',
      'answer': ['Max', 'Max', 'Max', 'Max'],
    }
  ];
  var _questionIndex = 0;

  void _answerQuestion() {
    if (_questionIndex < _questions.length) {
      print("We have more question!");
    }
    setState(() {
      _questionIndex = _questionIndex   1;
    });
    print(_questionIndex);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('My first App'),
          ),
          body: _questionIndex < _questions.length ?
          Quiz(
              answerQuestion: _answerQuestion,
              questions: _questions,
              questionIndex:_questionIndex
          ),
              :Result(),
    ),);
  }
}

hello guys, I am here with a dart program problem, please help me out I am having error at:

Quiz(answerQuestion: _answerQuestion,questions: _questions, questionIndex:_questionIndex

CodePudding user response:

Your Quiz constructor expect positional arguments, where you are passsing as named arguments,

Do it like

 Quiz( _answerQuestion, _questions, _questionIndex ),

If you like to use named argument, change your Quiz constructor.

Check more about using using-constructors

CodePudding user response:

I assume your Quiz widget has a constructor as follows:

Quiz(this.answerQuestion, this.questions, this.questionIndex)

These are using positional arguments. In your example however, you are trying to use named arguments. If you want to used named arguments, change your constructor to:

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

CodePudding user response:

It is because you have passed named arguments instead of positional arguments.

Quiz(
               _answerQuestion,
              _questions,
             _questionIndex
          ),
  • Related