You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter API请求报错:'List<dynamic>'不是'List<String>'的子类型

错误原因及解决方案:'List' is not a subtype of type 'List'

错误原因

  • 核心类型不匹配:
    1. Questions类中incorrectAnswers字段未声明具体类型,默认推断为dynamic。API返回的incorrect_answers是字符串数组,但经jsonDecode解析后会变成List<dynamic>类型。
    2. 在FutureBuilder代码中,你尝试将List<String>(由[correctAnswer]创建)与List<dynamic>(incorrectAnswers)拼接,两种列表类型不兼容,直接触发类型转换错误。
    3. 额外问题:options.shuffle是引用方法而非调用,正确写法应为options.shuffle();且FutureBuilder直接使用全局questions列表而非snapshot.data,易导致状态不一致。

解决方案

1. 修正Questions类的类型声明

明确指定incorrectAnswers为List<String>,从根源保证类型一致性:

class Questions {
  final String question;
  final String correctAnswer;
  final List<String> incorrectAnswers; // 声明为List<String>

  Questions({
    required this.question,
    required this.correctAnswer,
    required this.incorrectAnswers,
  });
}

2. 解析API数据时显式转换类型

在创建Questions实例时,将incorrect_answers从List<dynamic>强制转换为List<String>:

for (var eachquestion in responseData["results"]) {
  Questions fquestion = Questions(
    question: eachquestion["question"] as String,
    correctAnswer: eachquestion["correct_answer"] as String,
    // 显式转换为List<String>
    incorrectAnswers: List<String>.from(eachquestion["incorrect_answers"]),
  );
  questions.add(fquestion);
}

3. 修正FutureBuilder的数据源与方法调用

使用snapshot.data获取异步返回的列表,同时修复shuffle的调用方式:

FutureBuilder(
  future: getRequest(),
  builder: ((context, snapshot) {
    if (!snapshot.hasData) {
      return const Text("Loading...");
    } else {
      // 使用snapshot.data而非全局questions,避免状态不一致
      final currentQuestions = snapshot.data!;
      String answer = currentQuestions[indexVal].correctAnswer;
      List<String> options = [currentQuestions[indexVal].correctAnswer] +
          currentQuestions[indexVal].incorrectAnswers;
      options.shuffle(); // 调用shuffle方法,添加括号
      
      return Container(
        padding: const EdgeInsets.fromLTRB(15, 150, 15, 0),
        child: Column(
          children: (num == 10)
              ? [ScorePage(score: score)]
              : [
                  Container(
                    margin: const EdgeInsets.only(bottom: 30),
                    child: Center(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Expanded(
                            child: Text(
                              currentQuestions[indexVal].question,
                              style: const TextStyle(
                                  fontSize: 20, fontWeight: FontWeight.bold),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      indOption(nextQues: nextQues, optionVal: options[0]),
                      indOption(nextQues: nextQues, optionVal: options[1]),
                      indOption(nextQues: nextQues, optionVal: options[2]),
                      indOption(nextQues: nextQues, optionVal: options[3]),
                    ],
                  ),
                ],
        ),
      );
    }
  }),
)

内容的提问来源于stack exchange,提问作者Nishant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 12:35:17