【发布时间】:2021-07-08 01:53:03
【问题描述】:
我不断得到
未处理的异常:“String”类型不是“index”的“int”类型的子类型
在代码行
notificationtext.value =(jsonResponse['data']['content']);
对于 api 响应
{"err": 0, "data": [{"id": 1, "content": "Good Morning"}]}
谁能解释这行代码的原因和修复方法。
final ValueNotifier<String> notificationtext=ValueNotifier<String>("");
Future getNotification()async{
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
var jsonResponse = null;
var sid= sharedPreferences.getString("sid");
Map<String, String> headers = {
'Content-Type': 'application/json; charset=UTF-8',
'sid':'$sid',
};
var response = await http.get(Url,headers:headers
);
if(response.statusCode == 200) {
jsonResponse = json.decode(response.body);
if(jsonResponse['err']==0){
notificationtext.value =(jsonResponse['data']['content']);
print(notificationtext.value );
}
}else{
print("Not working");
}
}
Container notificationContent(){
return Container(
decoration:BoxDecoration(
color: Colors.white
),
child:ValueListenableBuilder(
valueListenable: notificationtext,
builder:(context,value, widget){
if(value==""){
return Text("Loading");
}else{
return Text(value);
}
},
)
);
}
【问题讨论】:
-
试试这个 jsonResponse['data'][0]['content']
标签: json api flutter dart mobile