how to sort json data in flutter?

ぐ巨炮叔叔 提交于 2020-12-15 06:21:04

问题


This is my Json data:

call_record: [
{
"machine_location":"Restaurant",
"allocation_date:"2008-08-31"
"status":"C",
"customer_address":"pune"
}
{
"machine_location":"Restaurant",
"allocation_date:"2008-08-31"
"status":"N",
"customer_address":"pune"
}
{
"machine_location":"Restaurant",
"allocation_date:"2008-08-31"
"status":"O",
"customer_address":"pune"
}]

In my app i want to show data in listview whose date is allocation_date=2008-08-31 Here is my code for display data in ListView:

    Widget todayCall(BuildContext context,resultCheck3){
          return Container(
           child: Column(
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Container(
                        child:Text("Today's Calls"),
                      ),
                    ],
                  ),
                    FutureBuilder(
                      future: api3,
                     builder: (context,snapshot){
                     if (snapshot.data != null) {
                      return Container(
                      child: ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (BuildContext conxt, int index) {
                      return Card(
                      child: Padding(
                      child: ExpansionTile(
                      title: Text('${snapshot.data[index]['customer_name']}') 
                      subtitle: Text('${snapshot.data[index]   ['error_message']}'"\n"'${snapshot.data[index]['allocation_date']}'
                              trailing: Icon(Icons.arrow_drop_down,size: 30.0,
                              children: <Widget>[
                              ListTile(
                              title: Text("Machine Model",style: TextStyle(fontSize:13)),
                              subtitle: Text('${snapshot.data[index]['machine_type']}')
                              ),
                              ListTile(
                              title: Text("Location",style: TextStyle(fontSize:13)),
                              subtitle: Text('${snapshot.data[index]['location_address']}')
                              ),
])))
}))
}}))])

I called this method in Body():

 body: Stack(
        children: <Widget>[
          ListView(
            children: <Widget>[
              callSummary(context,resultCheck2),
              todayCall(context,resultCheck3),
            ],
          ),
        ],
      ),

[now in my output all data in list view ][1]

[1]: https://i.stack.imgur.com/B3VIz.png .show data whose allocation_date is 2008-08-31


回答1:


You can create a filter icon above the listview, so you can use the date picker to select the date and based on the date you can filter the list and show it in your list view.

let me know if this works for you.

Thanks




回答2:


Before returning a widget in the future builder, you can check if the date condition is satisfied.

if (snapshot.data != null) {
                      return Container(
                      child: ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (BuildContext conxt, int index) {
                      if(snapshot.data[index]['allocation_date'] == '2008-08-31'){
                        return Card(.....
                        .
                        .
                        .
                      else{
                        return Container(height: 0); //return a container with zero height in the else case because you cant return a null and your return must be a widget
                      }
                        .
                        .
                        .


来源:https://stackoverflow.com/questions/59807999/how-to-sort-json-data-in-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!