puthumai nathan
puthumai nathan

Reputation: 37

The named parameter 'product' is required, but there's no corresponding argument. in flutter

** The named parameter 'product' is required, but there's no corresponding argument. **

import 'package:flutter/material.dart';
import 'package:shoppingapp/screens/home_screen.dart';
import 'package:shoppingapp/model/product.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        textTheme: Theme.of(context).textTheme.apply(bodyColor: Colors.black),
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ), //theme
      home: HomeScreen(),
    ); //materialapp
  }
}

** The named parameter 'product' is required, but there's no corresponding argument. **

** and this is my homescreen dart file **


import 'package:flutter/material.dart';
import 'package:shoppingapp/screens/components/body.dart';
import 'package:shoppingapp/model/product.dart';

class HomeScreen extends StatelessWidget {
  final Product product;
  const HomeScreen({Key? key, required this.product}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(),
      body: buildBody(),
    );
  }

  AppBar buildAppBar() {
    return AppBar(
      backgroundColor: Colors.white,
      elevation: 0,
      leading: IconButton(icon: Icon(Icons.arrow_back, color: Colors.black), onPressed: () {}), //iconbutton
      actions: <Widget>[
        IconButton(icon: Icon(Icons.search, color: Colors.black), onPressed: () {}), //iconbutton
        IconButton(icon: Icon(Icons.shopping_cart, color: Colors.black), onPressed: () {}), //iconbutton
      ], //<widget>
    ); //appBar
  }

  Body buildBody() {
    return Body(product: product);
  }
}

** and here is my product dart file **

import 'package:flutter/material.dart';

class Product {
  final String image, title, description;
  final int price, size, id;
  final Color color;

  Product({
    required this.id,
    required this.image,
    required this.title,
    required this.price,
    required this.description,
    required this.color,
    required this.size,
  });
}

List<Product> products = [
  Product(id: 1, title: "office code", price: 234, size: 12, description: dummyText, image: "assets/images/image_1.png", color: Color(0xFF3D82AE)), //product
  Product(id: 2, title: "office code", price: 234, size: 8, description: dummyText, image: "assets/images/image_2.png", color: Color(0xFFD3A984)),
  Product(id: 3, title: "office code", price: 234, size: 11, description: dummyText, image: "assets/images/image_3.png", color: Color(0xFF989493)),
  Product(id: 4, title: "office code", price: 234, size: 12, description: dummyText, image: "assets/images/image_4.png", color: Color(0xFFE6B398)),
  Product(id: 5, title: "office code", price: 234, size: 11, description: dummyText, image: "assets/images/image_5.png", color: Color(0xFFFB7883)),
  Product(id: 6, title: "office code", price: 234, size: 12, description: dummyText, image: "assets/images/image_6.png", color: Color(0xFFAEAEAE)),
];

String dummyText = "This company is an manufacturing and selling of hangbags products in premium quality ";

** refering my product file to other fileis the main problem **

Upvotes: 1

Views: 2466

Answers (2)

In HomeScreen constructor, you have defined Product parameter as required (mandatory) and you have not passed a Product object from MyApp when you are initializing the Home Screen Constructor.

If you add required annotation with parameter then you must have to provide/pass it's value.

Solution 1: Remove the annotation required for Product parameter from HomeScreen Constructor.

Solution 2: Pass Product object to HomeScreen from MyApp.

Upvotes: 0

venir
venir

Reputation: 2096

Your HomeScreen class requires a product property to be initialized when you instantiate its object.

This means that when you create a HomeScreen() object in your main.dart file, the compilator expects something like HomeScreen(product: MyProductOrSomething()).

Upvotes: 1

Related Questions