0

I have a list of several points of service in a List.

Within the PointsOfServices there is another object named Orders. How do i go about getting access to the Orders object to use it's data alongside the PointsOfService data?

Thanks

List question

Edit: I want to be able to use this data to produce a GridView that will enable me to display both data from PointOfServices and Orders.

Will i be able to use a Future for this and FutureBuilder when creating the GridView?

2 Answers 2

1

You can directly access it using the index

print(mainList[0].orders[0].id);

Will print the first pointOfService's first order's Id

Note: here mainList is the name of the list that contains all pointOfService and i assumed that you have id in each order

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

1 Comment

This is a great help, thank you!
1

Your question isn't clear as of what you want to exactly achieve, but to access list inside list , you can refer this ,

class PointOfService {
  final String name;
  final List<Order> orders;
  PointOfService({this.name, this.orders});
}

class Order {
  final String name;
  Order({this.name});
}

void main() {
  List<PointOfService> pointofServices = [
    PointOfService(
        name: "PointOfService 1",
        orders: [
          Order(name: "Order 1"),
          Order(name: "Order 2"),
        ]),
    PointOfService(
        name: "PointOfService 2",
        orders: [
          Order(name: "Order 3"),
          Order(name: "Order 4"),
        ])
  ];

  for (var pointOfService in pointofServices) {
    print("PointOfService name: ${pointOfService.name}");
    for (var order in pointOfService.orders) {
      print("Order name: ${order.name}");
    }
  }
}

This will output

PointOfService name: PointOfService 1
Order name: Order 1
Order name: Order 2
PointOfService name: PointOfService 2
Order name: Order 3
Order name: Order 4

Edit

For GridView you can do something like:

FutureBuilder<List<PointOfService>>(
    future: < Your future here >
    builder: (context, snapshot) {
        if (snapshot.hasData) {
        return  GridView.builder(
            gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                maxCrossAxisExtent: 200,
                childAspectRatio: 3 / 2,
                crossAxisSpacing: 20,
                mainAxisSpacing: 20),
            itemCount: snapshot.data.pointOfServices.length,
            itemBuilder: (context, index) {
                Order order = snapshot.data!.pointOfServices[index];
                return Column(
                       children:[
                           Text(order['name']),// You can access this way
                       );
              );
            }),
        } else {
        return Text("No data");
        }
    },
)

5 Comments

This is a great help, thank you. I have updated my question with a tad more detail.
@DeanBall I have updated my post to provide the possible way to reach your goal, make changes to the code provided according to your requirements, hope it helps!!
That is a help too, so i thank you for that. Bit of a digression to my original question but i'm not sure how to get the orders from the PointOfServices in my Future. Do i just return the PointOfServices list and that will allow me access to my Orders? Thanks
Yes exactly you need to get just the list of PointOfServices from the future,from there you can access the orders as mentioned in the code, then rest you can follow th code structure that i have provided, make sure you change my code to your requirements, it is just a template providing the structure of using gridview with the futureBuilder.
Brilliant, thank you. I'm new to all this, as you can see :) If i need to filter some data, can i still do that too? Using the where? So, Orders may container data i don't want them to see if the date doesn't equal Today.

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.