如何修复此错误未处理的异常:类型“字符串”不是“索引”类型“int”的子类型
Posted
技术标签:
【中文标题】如何修复此错误未处理的异常:类型“字符串”不是“索引”类型“int”的子类型【英文标题】:How can I fix this error Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index' 【发布时间】:2021-11-16 06:46:38 【问题描述】:我厌倦了向我的服务器发出获取 http 请求。在邮递员上,我成功获得了响应,但是当我尝试通过应用程序执行此操作时,出现此错误:
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:类型“String”不是“index”类型“int”的子类型
请有一种方法可以解决我尝试过但无法解决的问题。
这是我的代码:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() => runApp(TestApp());
class TestApp extends StatelessWidget
// This widget is the root of your application.
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
class MyHomePage extends StatefulWidget
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage>
String data;
var superheros_length;
@override
void initState()
// TODO: implement initState
super.initState();
getData();
void getData() async
int id = 1;
var token =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiODU2NDE3YTYwMTFjODFiNTQxYzkzOGY5YWFhNGU1OTY0MzYwYThmZGFiOGMwMGFmMGUwNzIxMTM2MTE5YmY5YjQwZjMyZGFkNjQ4YzdhOTIiLCJpYXQiOjE2MzEzNTQzNDguMDYxOTI5LCJuYmYiOjE2MzEzNTQzNDguMDYxOTM5LCJleHAiOjE2NjI4OTAzNDcuNjk0MDc0LCJzdWIiOiIyIiwic2NvcGVzIjpbXX0.JoGlxB2Htg3t3Fjpmtw6oKnuGXktN4n3g3xWa1Eh0U_TgKKCQv9OB04vaqkcaPo66EYvP89gxKuIIhuGJiEAXdTj0X_ScdNWwvB8Dki_CLQu7_MUyOXakxjz5jSe2xB-xnIB3-y8xc8rOkWZTf6QcPZm1eXUNGWelemvTafIzNm-YeGmpwBaXN2E04QUJ2EYBLu13h7T53rFG3wDBa2j10RS_FWtBlUxgYF5y-3aZNypIzdNI8dqFXuZl_0Crz4MHHFVgchFixJK_txoG2S08jqBCoaUuArFlxqVVL-wcMci6MITPCbiib-_2J4jydqwKgnxzGBqzX6hj0RXdT-BqolvsV5ZGX5sY8ANZR9AvGaJ3SQWbcNv5wVT_DlTqMSfjNwtaOYOocH_LTFNmhjQyTW8TqbiyJHuYbtKRDcwJ2X4kSK-IvA7nyn8xefcempuFYO9M6BD00E-WoiaKwEbxJa3iQsiHgcNziPI-3Lq7S-9CDNHYYiDrgyIrwRhcFuIklmFrsGl8tb08hReNLInlVcy_eAjYifjlJvJYYXdozuhAobcVxkR6z1Xn_N2Vta7OYMWdG4kCYCfR1uF6pypxvlxxoYcwO2Q3Aj3sVPdz03B8gYvmrx-F55qbXwfXdtT3LWUj_ziJOG53GLdDy8DsmKx0c1vDjuLpRz_EPhTYEQ';
String url = 'https://app.pyramidpharmacy.com/api/get-prescriptions-by-user-id';
http.Response response =
await http.get(url + '/' + id.toString(),
headers:
"Accept": "application/json",
"Content-type": "application/json",
HttpHeaders.authorizationHeader: 'Bearer $token'
);
// get("https://protocoderspoint.com/jsondata/superheros.json");
if (response.statusCode == 200)
data = response.body; //store response as string
setState(()
superheros_length = jsonDecode(
data)['id']; //get all the data from json string superheros
print("Http Response");
print(superheros_length.length); // just printed length of data
);
var venam = jsonDecode(data)[1]['screen_shot_path']; // Just to see if its getting the image
print(venam);
else
print(response.statusCode);
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text("Prescription List"),
backgroundColor: Colors.green,
),
body: ListView.builder(
itemCount: superheros_length == null ? 0 : superheros_length.length,
itemBuilder: (BuildContext context, int index)
return Card(
child: ListTile(
leading: Image.network(
jsonDecode(data)[index]['screen_shot_path'],
fit: BoxFit.fill,
width: 100,
height: 500,
alignment: Alignment.center,
),
title: Text(jsonDecode(data)[index]['title']),
subtitle: Text(jsonDecode(data)[index]['created_at']),
),
);
,
),
);
以下是我对邮递员测试的回复:
[
"id": 1,
"title": "Diabetics Prescription",
"screen_shot_path": "https://app.pyramidpharmacy.com/public/screenshots/41600.jpg",
"user_id": "1",
"status": "Pending",
"created_at": "2021-08-07T09:49:00.000000Z",
"updated_at": "2021-09-16T16:07:11.000000Z"
,
"id": 8,
"title": "DIabetics Challenge",
"screen_shot_path": "https://app.pyramidpharmacy.com/public/screenshots/25635.jpg",
"user_id": "1",
"status": "Pending",
"created_at": "2021-09-16T16:33:01.000000Z",
"updated_at": "2021-09-16T16:33:02.000000Z"
,
"id": 7,
"title": "DIabetics Challenge",
"screen_shot_path": "https://app.pyramidpharmacy.com/public/screenshots/16030.jpg",
"user_id": "1",
"status": "Pending",
"created_at": "2021-09-16T16:32:42.000000Z",
"updated_at": "2021-09-16T16:32:44.000000Z"
,
"id": 6,
"title": "Covid 19",
"screen_shot_path": "https://app.pyramidpharmacy.com/public/screenshots/43563.jpg",
"user_id": "1",
"status": "Pending",
"created_at": "2021-09-16T16:25:26.000000Z",
"updated_at": "2021-09-16T16:26:45.000000Z"
]
任何帮助都会有很大帮助。谢谢。
【问题讨论】:
【参考方案1】:这是因为返回的 JSON 数据是一个项目列表。
据我所知,您无法通过 ['id'] 直接检索所有 id,它需要一个索引。
你必须像这样遍历列表:
setState(()
List jsonList = jsonDecode(data);
List ids = [];
for (int i = 0; i < jsonList.length; i++)
ids.add(jsonList[i]['id']);
print("Http Response");
print(ids.length); // just printed length of data
);
您还可以在循环中以类似的方式获取 JSON 的其他部分。
【讨论】:
非常感谢它的工作以上是关于如何修复此错误未处理的异常:类型“字符串”不是“索引”类型“int”的子类型的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 中的错误:未处理的异常:“Null”类型不是“String”类型的子类型
如何修复颤振中的“未处理的异常:用于空值的空检查运算符”错误?