ValueNotifier 未处理异常:类型 'String' 不是 'index' 类型 'int' 的子类型
ValueNotifier Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
我不断得到
Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
代码行
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);
}
},
)
);
}
notificationtext.value =(jsonResponse['data'][0]['content']);
Kei Credo 回答。我不知道如何在评论中支持你的回答。
我不断得到
Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
代码行
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);
}
},
)
);
}
notificationtext.value =(jsonResponse['data'][0]['content']);
Kei Credo 回答。我不知道如何在评论中支持你的回答。