【问题标题】:The argument type 'Map<String, Object>' can't be assigned to the parameter type 'String'参数类型“Map<String, Object>”不能分配给参数类型“String”
【发布时间】:2021-08-20 15:19:03
【问题描述】:

import 'package:learnflutter/questionMain.dart';
import 'questionButton.dart';

class mainQuestions extends StatelessWidget {
  List aQuestions;
  int questionIndex;
  Function answerQuestion;

  mainQuestions(
      {required this.aQuestions,
      required this.answerQuestion,
      required this.questionIndex});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Question(
          aQuestions[questionIndex]['aQuestion'],
        ),
        ...(aQuestions[questionIndex]['answers'] as List<Map<dynamic, dynamic>>)
            .map((answer) {
          return Answer(answerQuestion, answer);
        })
      ],
    );
  }
}

我的问题是它一直在说: 参数类型“Map”不能分配给参数类型“String” 我是新来的颤振有人能帮助我吗?谢谢! (不是说要使用 HTML 只是不知道如何添加代码

...(aQuestions[questionIndex]['answers'] as List<Map<dynamic, dynamic>>)
            .map((answer) {
          return Answer(answerQuestion, answer);

【问题讨论】:

    标签: arrays list flutter dart


    【解决方案1】:

    我认为将解决的问题是将答案提取到一个单独的变量中,并在最后调用toList。这并没有对我产生任何编译错误:

    List<Widget> _answers() {
      List<Map<dynamic, dynamic>> answers = aQuestions[questionIndex]['answers'] as List<Map<dynamic, dynamic>>;
      return answers.map(answer => Answer(answerQuestion, answer)).toList();
    }
    
    return Column(
      children: [
        Question(aQuestions[questionIndex]['aQuestion']),
        ..._answers(),
      ]
    

    【讨论】:

      【解决方案2】:

      map 函数返回一个可迭代的,而不是一个列表。这就是为什么在映射之后,您必须通过调用其 toList() 函数将其转换为列表。 方法如下:

      import 'package:learnflutter/questionMain.dart';
      import 'questionButton.dart';
      
      class mainQuestions extends StatelessWidget {
        List aQuestions;
        int questionIndex;
        Function answerQuestion;
      
        mainQuestions(
            {required this.aQuestions,
            required this.answerQuestion,
            required this.questionIndex});
      
        @override
        Widget build(BuildContext context) {
          return Column(
            children: [
              Question(
                aQuestions[questionIndex]['aQuestion'],
              ),
              ...(aQuestions[questionIndex]['answers'] as List<Map<dynamic, dynamic>>)
                  .map((answer) {
                return Answer(answerQuestion, answer);
              }).toList()
            ],
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-10-20
        • 2021-07-28
        • 2021-09-06
        • 2021-08-03
        • 2021-11-07
        • 2023-02-18
        • 2021-05-17
        • 2020-12-15
        相关资源
        最近更新 更多