首页 > 解决方案 > 从列表中获取时,颤振类型'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,问题仍然存在。

在查看了一些具有类似答案的问题后,我仍然找不到解决方案。我该如何解决这个问题?如果当前信息不够,我可以提供更多代码。

标签: flutterdart

解决方案


结果

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);
  
}

推荐阅读