Dinesh Gosavi
Dinesh Gosavi

Reputation: 21

Too many positional arguments: 0 expected, but 1 found

Error is in _AddToCart(catalog)
following a tutorial but in tutorial there is no error why getting error here please solve this I am trying to pass catalog details to _AddToCart

error: Too many positional arguments: 0 expected, but 1 found. Try removing the extra positional arguments, or specifying the name for named arguments

class CatalogList extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        shrinkWrap: true,
        itemCount: CatelogModel.items!.length,
        itemBuilder: (context, index) {
          final catalog = CatelogModel.items![index];
          return InkWell(
            onTap: () => Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => HomeDetailPage(catalog: catalog),
              ),
            ),
            child: CatalogItem(catalog: catalog),
          );
        });
  }
}

class CatalogItem extends StatelessWidget {
  final Item catalog;  #Catelog is accessed here#
  
  const CatalogItem({Key? key, required this.catalog}) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return VxBox(
      child: Row(
        children: [
          Hero(
            tag: Key(catalog.id!.toString()),
            child: CatalogImage(
              image: catalog.image,
            ),
          ),
          Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  catalog.name!.text.lg.color(context.accentColor).bold.make(),
                  catalog.desc!.text.sm.color(Colors.grey).make(),
                  10.heightBox,
                  ButtonBar(
                    alignment: MainAxisAlignment.spaceBetween,
                    buttonPadding: EdgeInsets.zero,
                    children: [
                      "\$${catalog.price!}".text.bold.make(),
                      _AddToCart(catalog),  ##Getting Error here##
                    ],
                  ).pOnly(right: 8.0),
                ],
              )),
        ],
      ),
    ).color(context.cardColor).rounded.square(150).make().py8();
  }
}

class _AddToCart extends StatefulWidget {
  const _AddToCart({
    Key? key,
  }) : super(key: key);
  
  @override
  __AddToCartState createState() => __AddToCartState();
}

class __AddToCartState extends State<_AddToCart> {
  bool isAdded = false;
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () async {
        isAdded = isAdded.toggle();
        final _catalog = CatelogModel();
        final _cart = CartModel();
        _cart.add(_catalog);
        setState(() {});
      },
      style: ButtonStyle(
          backgroundColor: MaterialStateProperty.all(
            MyTheme.darkBluishColor,
          ),
          shape: MaterialStateProperty.all(StadiumBorder())),
      child: isAdded ? Icon(Icons.done) : "Buy".text.make(),
    );
  }
}  

Upvotes: 0

Views: 393

Answers (1)

Adelina
Adelina

Reputation: 11871

You are passing catalog to _AddToCart class, but constructor does not have any positional arguments

class _AddToCart extends StatefulWidget {  
  final CatelogModel  catalog;
  _AddToCart(this.catalog, {  
    Key? key,  
  }) : super(key: key);  
  ....

Upvotes: 1

Related Questions