【问题标题】:Closure call with mismatched arguments: function '_MyAppState.build.<anonymous closure>'参数不匹配的闭包调用:函数'_MyAppState.build.<匿名闭包>'
【发布时间】:2021-09-29 22:06:53
【问题描述】:

我在按下按钮时遇到此错误(编译时没有错误) 它在 ()=> answerQuestions(answer['score'])

处显示错误

匿名函数出了点问题,但我无法理解

The following NoSuchMethodError was thrown while handling a gesture:
Closure call with mismatched arguments: function '_MyAppState.build.<anonymous closure>'
Receiver: Closure: () => (int) => void
Tried calling: '_MyAppState.build.<anonymous closure>(8)'
Found: '_MyAppState.build.<anonymous closure>() => (int) => void'
import 'package:flutter/material.dart';

import './question.dart';
import './answer.dart';

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

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

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

【问题讨论】:

    标签: flutter closures gesture


    【解决方案1】:

    试试这个

    import './question.dart';
    import './answer.dart';
    
    class Quiz extends StatelessWidget {
      final List<Map<String, Object>> questions;
      final int questionIndex;
      final Function answerQuestions;
    
      Quiz({
        required this.questions,
        required this.answerQuestions,
        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(() => answerQuestions(answer['score']), answer['text'] );
            }).toList()
          ],
        );
      }
    }
    

    【讨论】:

    • 删除“作为字符串”后,问题和答案列表中都会弹出此错误参数类型“对象?”不能分配给参数类型“字符串”。
    猜你喜欢
    • 2021-12-09
    • 2021-12-10
    • 1970-01-01
    • 2022-01-14
    • 2022-12-21
    • 2013-12-01
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    相关资源
    最近更新 更多