如何在flutter中索引添加到listview.builder中的列表的JSON数据?

时间:2020-10-11 11:58:17

标签: json flutter flutter-listview

我能够在Flutter应用程序中将JSON数据添加到列表中。但是在Listview.builder中使用更新的列表时,出现以下错误

类型'String'不是'索引'的类型'int'的子类型

用于将数据添加到列表的代码

 List list = jsonResponse['notification'];
 List notificationsList.addAll(list);

Listview.Builder中的实现

Text(notificationsList[index]['order_id'])

实际上,此错误与[index]

有关

检索到的数据为String数据类型

我该如何解决?

如果需要更多信息,请告诉我

我正在使用的JSON数据

{
    "error": "false",
    "notification": [
        {
            "rn": "1",
            "order_id": "224",
            "company_details": {
                "code": "2",
            }
        },
        {
            "rn": "2",
            "order_id": "219",
            "company_details": {
                "code": "3",
            }
        },
        {
            "rn": "3",
            "order_id": "213",
            "company_details": {
                "code": "3",
            }
        },
        {
            "rn": "4",
            "order_id": "209",
            "company_details": {
                "code": "4",
            }
        },
        {
            "rn": "5",
            "order_id": "204",
            "company_details": {
                "code": "3",
            }
        },
        {
            "rn": "6",
            "order_id": "199",
            "company_details": {
                "code": "3",
            }
        },
        {
            "rn": "7",
            "order_id": "193",
            "company_details": {
                "code": "3",
            }
        }
    ],
}

Listview实现在下面

 ListView.separated(
            separatorBuilder: (context, index){
              return Divider();
            },
            controller: _scrollController,
            itemCount: notificationsList.length,
            itemBuilder: (context, index) {

              return  Padding(
                padding: const EdgeInsets.fromLTRB(8.0, 7.0, 8.0, 0.0),
                child: Column(
                  children: <Widget>[
                    ListTile(
                     
                      leading:ClipRRect(
                        borderRadius: BorderRadius.circular(10),
                        child: Image.asset('images/appstore.png', width: 50, height: 50)
                    ) ,
                    title: Text(
                      notificationsList[index]['title'].toString(),
                      style: TextStyle(fontWeight: FontWeight.bold),) ,
                                   subtitle: notificationsList[index]['message']!= null?Text(
                                     notificationsList[index]['message'],
                                     style: TextStyle(fontWeight: FontWeight.bold),) :Text(''),
                      trailing: notificationsList[index]['created_at']!= null?Text(
                        notificationsList[index]['created_at'],
                        style: TextStyle(fontWeight: FontWeight.bold),) :Text(''),


                    ),





                  ],
                ),
              );

            },

          )

0 个答案:

没有答案