flutter - 从列表中获取时,颤振类型'String'不是'index'类型'int'的子类型
问题描述
尝试编译我的程序时,出现此错误:
======== Exception caught by widgets library =======================================================
The following _TypeError was thrown building:
type 'String' is not a subtype of type 'int' of 'index'
When the exception was thrown, this was the stack:
#0 _HistoryPageState.builder.<anonymous closure>.<anonymous closure> (package:arkadas_testi/history.dart:54:38)
#1 SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:455:22)
#2 SliverMultiBoxAdaptorElement._build (package:flutter/src/widgets/sliver.dart:1213:28)
#3 SliverMultiBoxAdaptorElement.createChild.<anonymous closure> (package:flutter/src/widgets/sliver.dart:1226:55)
#4 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2535:19)
...
====================================================================================================
这是我的代码:
var item = list[0];
int puan = int.parse(item["sonuc"]);
这list
看起来像:[{arkadaşİsim: emir, kullaniciİsim: emir, sonuc: 76, yazi: Kesinlikle Arkadaş Ol}]
。item["sonuc"]
是一个字符串。
即使将“sonuc”转换为int,问题仍然存在。
在查看了一些具有类似答案的问题后,我仍然找不到解决方案。我该如何解决这个问题?如果当前信息不够,我可以提供更多代码。
解决方案
void main() {
List list = [{'arkadaşİsim': 'emir', 'kullaniciİsim': 'emir', 'sonuc': 76, 'yazi': 'Kesinlikle Arkadaş Ol'}];
double sum = 0;
String string = 'String Sonuc:';
sum = list[0]['sonuc'] + 10;
//string += list[0]['sonuc']; => error, list[0]['sonuc'] => int
string = string + list[0]['sonuc'].toString();
print(sum);
print(string);
}
推荐阅读
- python - 如果在 defaultdict 中找不到键,则返回最大键的值
- angular - 将输入的文本与组合框选项进行比较
- github - 跨 GitHub 组织的看板
- c# - 接口方法不能实现另一个接口
- jquery - 括号/数组的jQuery选择器问题
- python - 如何每天在运行时更新模型/向量?
- python - 在嵌套的 RFECV 中缩放、然后调整超参数的管道。我究竟做错了什么?
- botframework - 如何从 MS Teams 消息扩展的 Bot 实现类将“类似调试”的消息记录到浏览器控制台?
- java - 部署到 Heroku 时 Java REST api 不起作用
- multithreading - 在 goroutine 中使用 io.Copy 时锁定