【问题标题】:Flutter "The expression doesn't evaluate to a function, so it can't be invoked."Flutter“表达式不计算为函数,因此无法调用。”
【发布时间】:2022-01-12 11:53:24
【问题描述】:

我正在构建一个应用程序来练习我的 Flutter 技能。该应用程序是一个测验应用程序。我试图让用户可以添加问题。我有一个变量,它是一个名为:问题的列表。我还有另一个变量叫做:answers(也是一个列表)。所以它们是分开的,但是,我有一个 questionIndex 变量,以便显示问题并分配正确答案,我使用索引来定义哪个列表项是相关的。

现在我有一个问题....我成功地尝试在我的 statefulwidget 中添加带有函数的问题文本,但是,尝试向答案列表添加答案不起作用。它给了我错误:“表达式不计算为函数,因此无法调用它。”

你会如何解决这个问题?非常感谢您的意见!

带有 StatefulWidget 的文件(包含添加问题和答案的功能):

import 'questionText.dart';
import 'buttons.dart';
import 'end.dart';
import '../pages/add_questions.dart';
import '../model/question_class.dart';
import '../model/Answer_class.dart';

class QuizPage extends StatefulWidget {
 @override
 State<QuizPage> createState() => _QuizPageState();
}

class _QuizPageState extends State<QuizPage> {
 int questionIndex = 0;
 int points = 0;

 List<Widget> scoreKeeper = [];

 var _questions = [
   {
     'Victor loves computers',
   },
   'Victor is learning Flutter',
   'Victor\'s favorite team is Manchester United',
   'Victor\'s favorite team is Chelsea'
 ];

 List<bool> answers = [
   true,
   true,
   false,
   true,
 ];

 void _addIndex() {
   setState(() {
     questionIndex = questionIndex + 1;
   });
 }

 late bool userAnswer;

 void _setUserAnswerTrue() {
   setState(() {
     userAnswer = true;
   });
 }

 void _setUserAnswerFalse() {
   setState(() {
     userAnswer = false;
   });
 }

 void _checkIfCorrect() {
   bool correctAnswer = answers[questionIndex];
   setState(() {
     correctAnswer == userAnswer ? points = points + 1 : points = points;
     print(points);
   });
   setState(
     () {
       correctAnswer == userAnswer
           ? scoreKeeper.add(
               Icon(
                 Icons.check,
                 color: Colors.green,
               ),
             )
           : scoreKeeper.add(
               Icon(
                 Icons.close,
                 color: Colors.red,
               ),
             );
     },
   );
 }

 void restartQuiz() {
   setState(() {
     questionIndex = 0;
     scoreKeeper.clear();
   });
 }

 void addNewQuestion(String txTitle) {
   final newTx = Question(
     title: txTitle,
   );

   setState(() {
     _questions.add(newTx.toString());
   });
 }

 void addNewBool(bool txAnswer) {
   final newBool = Answer(
     correctAnswer: txAnswer,
   );
   setState(() {
     answers.add(newBool());
   });
 }

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       backgroundColor: Colors.grey.shade900,
       actions: [
         IconButton(
           onPressed: () {
             Navigator.push(
               context,
               MaterialPageRoute(
                 builder: (context) =>
                     AddQuestions(addNewQuestion, addNewBool),
               ),
             );
           },
           icon: Icon(Icons.add),
         ),
       ],
     ),
     backgroundColor: Colors.grey.shade900,
     body: SafeArea(
         child: Padding(
       padding: const EdgeInsets.all(10.0),
       child: questionIndex < _questions.length
           ? Column(
               mainAxisAlignment: MainAxisAlignment.spaceBetween,
               crossAxisAlignment: CrossAxisAlignment.stretch,
               children: [
                 QuestionText(_questions, questionIndex),
                 QuButtons(_setUserAnswerTrue, _setUserAnswerFalse,
                     _checkIfCorrect, _addIndex),
                 Padding(
                   padding: const EdgeInsets.symmetric(horizontal: 8.0),
                   child: Row(
                     children: scoreKeeper,
                   ),
                 ),
               ],
             )
           : EndOfQuiz(scoreKeeper, restartQuiz),
     )),
   );
 }
}

我的文件包含文本输入:

import 'package:flutter/material.dart';

class AddQuestions extends StatelessWidget {
  final titleController = TextEditingController();
  final answerController = TextEditingController();
  final Function addQu;
  final Function addBoo;
  AddQuestions(this.addQu, this.addBoo);

  void submitInput() {
    final enteredTitle = titleController.text;
    final enteredBool = answerController.text.toLowerCase();

    addQu(
      enteredTitle,
    );
    addBoo(
      enteredBool,
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.grey.shade900,
        appBar: AppBar(
          backgroundColor: Colors.grey.shade900,
          leading: IconButton(
            icon: Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Padding(
              padding: const EdgeInsets.all(20),
              child: TextField(
                controller: titleController,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'Question Text',
                ),
                onSubmitted: (_) => submitInput(),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(20),
              child: TextField(
                controller: answerController,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'True/False',
                ),
                onSubmitted: (_) => submitInput(),
              ),
            ),
            FlatButton(
              onPressed: () {
                submitInput;
              },
              child: Text('Add button'),
            )
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: list flutter function


    【解决方案1】:

    答案列表是一个布尔数组,您将 newBool​​() 添加为一个函数,它是 Answer 类的对象。 newBool​​() 是一个函数而不是布尔值。并且列表需要添加一个布尔值。

    【讨论】:

    • 更改应该在这一行 answers.add(newBool​​());
    • 我应该添加/更改什么?
    • 如何将 newBool​​ 更改为布尔值?
    • answers.add(txAnswer);
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 2023-02-02
    相关资源
    最近更新 更多