簡體   English   中英

參數“answerQuestion”的值不能為“null”,因為它的類型為“Function”,但隱含的默認值為“null”

[英]The parameter 'answerQuestion' can't have a value of 'null' because of its type 'Function', but the implicit default value is 'null'

import 'package:flutter/material.dart';

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

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

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

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

顯示這個

終端顯示這個

lib/quiz.dart:12:20:錯誤:參數“answerQuestion”的值不能為“null”,因為它的類型為“Function”,但隱含的默認值為“null”。

  • “功能”來自“飛鏢:核心”。 嘗試添加顯式的非“null”默認值或“required”修飾符。 @required this.answerQuestion,^^^^^^^^^^^^^lib/quiz.dart:13:20:錯誤:參數“問題”不能具有“空”值,因為它鍵入“List<Map<String, dynamic>>”,但隱含的默認值為“null”。
  • “列表”來自“飛鏢:核心”。
  • “地圖”來自“飛鏢:核心”。 嘗試添加顯式的非“null”默認值或“required”修飾符。 @required this.questions, ^^^^^^^^^ lib/quiz.dart:14:20: 錯誤:參數'questionIndex'不能有'null'的值,因為它的類型'int',但隱含的默認值為“null”。 嘗試添加顯式的非“null”默認值或“required”修飾符。 @required this.questionIndex, ^^^^^^^^^^^^^

@required注釋現在已經過時了,我們通過required關鍵字獲得了實際的編譯器支持:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM