Ahmed Al-Khateeb
Ahmed Al-Khateeb

Reputation: 45

A value of type 'Object?' can't be assigned to a variable of type 'ProductDetailsArguments'

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,
  ),

Upvotes: 0

Views: 328

Answers (1)

Peter Koltai
Peter Koltai

Reputation: 9764

Instead of:

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

try:

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

Upvotes: 2

Related Questions