2

I have the following json

{
  "time": [
    [
      {
        "date": "1",
        "available": "2"
      },
      {
        "date": "2",
        "available": "2"
      },
      {
        "date": "3",
        "available": "3"
      }
    ],
    [
      {
        "date": "1",
        "available": "2"
      },
      {
        "date": "2",
        "available": "2"
      },
      {
        "date": "3",
        "available": "2"
      }
    ]
  ]
}



This json has a time array which contains 2 lists of objects. This number is variable I want to makedisplay these as list views inside of another listview

1
  • Hi there - it might help if you can include the code that you have tried, which you would like to have help on. Commented Aug 3, 2021 at 11:57

3 Answers 3

2

There are two steps in this process. Preferably you'll start by mapping this json to a data structure. Then you can display the objects. There's a working example you can try out in your browser at the bottom of the answer

Mapping to a data structure

Let's say you'll have the following class:

class TimeSlot {
  final String date;
  final String available;

  TimeSlot(this.date, this.available);
}

You can manually implement a fromJson method, or use the json_serializable package and simply annotate the class. I'll do it manually in this example.

TimeSlot.fromJson(Map<String, dynamic> json)
    : date = json['date'],
        available = json['available'];

Now that you have this set up, you can map your json string to this structure. Having a structure of nested arrays makes this quite complex.

import 'dart:convert';

final json = jsonDecode(myJson);
final timeJson = (json['time'] as List<dynamic>).cast<List<dynamic>>();
final List<List<TimeSlot>> time = timeJson.map((timeGroup) {
  return timeGroup.map((item) {
    return TimeSlot.fromJson(item as Map<String, dynamic>);
  }).toList();
}).toList();

Displaying the data

Having nested ListView widgets might cause issues because of nesting their scrolling behavior. Here's an example how you can use both ListView and Column for this.

return ListView.separated(
  itemCount: time.length,
  separatorBuilder: (_, index) => Divider(),
  itemBuilder: (_, outerIndex) {
    final group = time[outerIndex];
    return Column(
      children: 
        group.map((slot) => ListTile(
          title: Text("date: ${slot.date}"),
          subtitle: Text("available: ${slot.available}"),
        )).toList(),
    );
  }
);

Working example

I've made a working example here on DartPad: https://dartpad.dev/?id=b8d6d72a1359c6b9f638480d667f5cf9&null_safety=true

Additional resources

Check out the section on JSON and serialization on the Flutter dev site.

Sign up to request clarification or add additional context in comments.

1 Comment

Sure thing. Best of luck :) feel free to upvote if it helped ;-)
0

use this:

var data = json.decode(YOUR_RESPONSE);
List<List<Map, dynamic>> timeList= json.decode(data["time"]);

then you can use them inside a ListView.builder:

ListView.builder{
   itemCount:timeList.length,
   itemBuilder: (ctx, index){
      List<Map, dynamic> temp = timeList[index];
      return ListView.builder{
         itemCount:temp.length,
         itemBuilder: (ctx1, index1){
               // now you have access to each element by temp[index1] an you 
                // can return it
      }
    }
}

6 Comments

work great,,, but i cant scroll item even i put it in scrollview
Share your code, so I can help you with that
onlinenotebook.net/Gmur6qUtjUYwO6K please here is my code
did you tried to put your Appoinmentd in a sized container with fixed height, so you can check that this problem is related to your Appoinmentd widget or the main one! let me know the result. if it does not scroll again, we should check the child widget.
Can possible come any desk?
|
0

I think nested ListView is not give the best performance. and if we use column for inner list then this also not good in terms of performance, Bcz if inner list has 1000 item then all View are create at some time.

You can convert you NestedList data in to FlatterdList and then use one ListView.

Model class

class TimeSlot {
  final String date;
  final String available;

  TimeSlot(this.date, this.available);
}

Parse json to model

final json = jsonDecode(myJson);
final timeJson = (json['time'] as List<dynamic>).cast<List<dynamic>>();
final List<List<TimeSlot>> time = timeJson.map((timeGroup) {
  return timeGroup.map((item) {
    return TimeSlot.fromJson(item as Map<String, dynamic>);
  }).toList();
}).toList();

Convert time into flatteredTime

List<TimeSlot> flatteredTime = time.expand((i) => i).toList();

Then display

ListView.builder(
         itemCount:flatteredTime.length,
         itemBuilder: (ctx1, index1){
               // now you have access to each element by flatteredTime[index1] an you 
                // can return it
      )

And we can also use mixed-list like given in official doc

https://flutter.dev/docs/cookbook/lists/mixed-list

But dont know which is the best way. any one has best solution then pls comment here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.