I am trying to get the rating from a list I made and made an argument whenever I try getting the rating to apply it so I made a ModalRoute to try and fetch it but I am getting an error.

class ProductDetails extends StatelessWidget {
  static String routeName = "/Product_details";

  const ProductDetails({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final ProductDetailsArguments arguments = ModalRoute.of(context)!.settings.arguments;
    return Scaffold(
      backgroundColor: Color(0XFFF5F6F9),
      appBar: CustomAppBar(arguments.product.rating),
      body: Body(),
    );
  }
}

class ProductDetailsArguments {
  final Product product;

  ProductDetailsArguments({required this.product});
}

List<Product> demoProducts = [
  Product(
    id: 1,
    images: [
      "assets/images/mine.png",
      "assets/images/mine.png",
      "assets/images/mine.png",
      "assets/images/mine.png",
    ],
    colors: [
      Color(0xFFF6625E),
      Color(0xFF836DB8),
      Color(0xFFDECB9C),
      Colors.white,
    ],
    title: "Wireless Controller for PS4™",
    price: 64.99,
    description: description,
    rating: 4.8,
    isFavourite: true,
    isPopular: true,
  ),


Solution 1: Peter Koltai

Instead of:

final ProductDetailsArguments arguments = ModalRoute.of(context)!.settings.arguments;

try:

final ProductDetailsArguments arguments = ModalRoute.of(context)!.settings.arguments as ProductDetailsArguments;