Reputation: 31
I keep getting
Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
on the code line
notificationtext.value =(jsonResponse['data']['content']);
and for the api response
{"err": 0, "data": [{"id": 1, "content": "Good Morning"}]}
can someone explain the reason and a fix for this line of code.
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);
}
},
)
);
}
Upvotes: 0
Views: 574
Reputation: 31
notificationtext.value =(jsonResponse['data'][0]['content']);
Kei Credo answer . i dont know how to upvote your answer in the comment.
Upvotes: 1