【问题标题】:NoSuchMethodError while using map in flutter在颤振中使用地图时出现NoSuchMethodError
【发布时间】:2021-05-23 13:26:03
【问题描述】:

请帮助..在模拟器上运行应用程序时发生错误....NoSuchMethodErrorScreen Shot of my code and Emulator

【问题讨论】:

  • questions[_questionIndex]['answers'] 为空。使用collection if 来避免它。
  • 在哪里使用它。请解释
  • 我添加了一个答案,看看吧。
  • 我已经添加了一个答案,看看是否清楚。如果不告诉我哪个部分让你感到困惑。

标签: flutter dart mapping nosuchmethoderror


【解决方案1】:

由于您的questions[_questionIndex]['answers'] 将变为null,因此您不能在其上调用map 函数。这就是错误出现的原因。

因此,在您尝试使用map 访问它之前,请检查List 是否确实存在。

你可以在你的情况下使用集合来做到这一点,如果,

children: [
  Question(questions[_questionIndex]['questionText']),
  if (questions[_questionIndex]['answers'] != null)
    ...(questions[_questionIndex]['answers'] as List<String>)
      .map((answer) => Answer(_answerQuestion, answer)).toList(),
],

【讨论】:

  • 解决了吗?
猜你喜欢
  • 2019-07-14
  • 1970-01-01
  • 2020-10-14
  • 2021-01-26
  • 2020-09-21
  • 2021-09-08
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
相关资源
最近更新 更多