【问题标题】:ValueNotifier Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'ValueNotifier 未处理的异常:类型“String”不是“index”类型“int”的子类型
【发布时间】: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


【解决方案1】:
notificationtext.value =(jsonResponse['data'][0]['content']);

Kei Credo 答案。我不知道如何在评论中支持你的答案。

【讨论】:

    猜你喜欢
    • 2020-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-22
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多