在尝试编译我的程序时,我收到此错误:
======== 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,问题仍然存在。
在浏览了一些答案相似的问题后,我仍然找不到解决方案。我该如何解决这个问题?如果当前信息不够,我可以提供更多代码。
答案 0 :(得分:0)
我认为这里的问题是您将 puan 传递到某个文本字段,而现在 puan 是一个 int。 反正这是我的猜测,因为你没有很好地解释这个问题
答案 1 :(得分:0)
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);
}