How To Cast List<dynamic> To List<Object> In Flutter


 Recently, I started working on a Flutter app, specifically a payment bill app, in which I needed to cast a `List<dynamic>` to a `List<Object>`. I have successfully completed this task, and in this post, I will discuss how you can convert a `List<dynamic>` to a `List<Object>` in Flutter.


class Order {
  int id;
  String productName;
  String productDescription;
  double soldPrice;
  int quantity;
  DateTime createdAt;

  Order({
    required this.id,
    required this.productName,
    required this.productDescription,
    required this.soldPrice,
    required this.quantity,
    required this.createdAt,
  });
}

void main() {
  // Suppose we have a list of dynamic objects representing orders
  List<dynamic> dynamicOrders = [
    {
      'id': 1,
      'productName': 'Product 1',
      'productDescription': 'Description 1',
      'soldPrice': 10.0,
      'quantity': 2,
      'createdAt': DateTime.now(),
    },
    {
      'id': 2,
      'productName': 'Product 2',
      'productDescription': 'Description 2',
      'soldPrice': 15.0,
      'quantity': 1,
      'createdAt': DateTime.now(),
    },
  ];

  // We want to cast this list of dynamic objects to a list of Order objects
  List<Order> orders = dynamicOrders.map((dynamic order) {
    return Order(
      id: order['id'],
      productName: order['productName'],
      productDescription: order['productDescription'],
      soldPrice: order['soldPrice'],
      quantity: order['quantity'],
      createdAt: order['createdAt'],
    );
  }).toList();

  // Now 'orders' contains a list of Order objects
}

  • We have define a class Order to represent an order with properties like id, productName, productDescription, soldPrice, quantity, and createdAt and then initialize a list dynamicOrders containing dynamic objects representing orders. These objects have fields like 'id', 'productName', etc.
  • Using the map function to convert each dynamic object in dynamicOrders to an Order object. 
  • And then finally, we convert the mapped iterable to a list using toList() and assign it to orders, which now contains a list of Order objects and in this way, we've successfully cast a List<dynamic> to a List<Order> in Flutter.


We can create a reusable function to convert a List<dynamic> to a List<Order> in our Flutter app.
Let's define a function convertDynamicListToOrderList that takes a List<dynamic> as input and returns a List<Order> and then inside the function, we use the same logic as before to map each dynamic object to an Order object and now we can call this function wherever we need to convert a list of dynamic objects to a list of Order objects, making our code reusable.

class Order {
  int id;
  String productName;
  String productDescription;
  double soldPrice;
  int quantity;
  DateTime createdAt;

  Order({
    required this.id,
    required this.productName,
    required this.productDescription,
    required this.soldPrice,
    required this.quantity,
    required this.createdAt,
  });
}

List<Order> convertDynamicListToOrderList(List<dynamic> dynamicList) {
  return dynamicList.map((dynamic order) {
    return Order(
      id: order['id'],
      productName: order['productName'],
      productDescription: order['productDescription'],
      soldPrice: order['soldPrice'],
      quantity: order['quantity'],
      createdAt: order['createdAt'],
    );
  }).toList();
}

void main() {
  // Suppose we have a list of dynamic objects representing orders
  List<dynamic> dynamicOrders = [
    {
      'id': 1,
      'productName': 'Product 1',
      'productDescription': 'Description 1',
      'soldPrice': 10.0,
      'quantity': 2,
      'createdAt': DateTime.now(),
    },
    {
      'id': 2,
      'productName': 'Product 2',
      'productDescription': 'Description 2',
      'soldPrice': 15.0,
      'quantity': 1,
      'createdAt': DateTime.now(),
    },
  ];

  // We can now use our reusable function to convert the dynamic list to an Order list
  List<Order> orders = convertDynamicListToOrderList(dynamicOrders);

  
}