尝试删除多余的位置参数,或为命名参数指定名称

时间:2020-08-20 14:51:59

标签: flutter

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:testapp/cart.dart';
import 'package:testapp/cart_item.dart';

class CartScreen extends StatelessWidget {
  @override
Widget build(BuildContext context) {
    final cart = Provider.of<Cart>(context);
return new Scaffold(
  appBar: new AppBar(
    title: new Text(
      'Cart Screen',
      style:
          TextStyle(fontSize: 30.0, color: Theme.of(context).accentColor),
    ),
  ),
  body: Column(
    children: <Widget>[
      ListView.builder(
          itemCount: cart.items.length,
          itemBuilder: (ctx, i) => CartPdt(
              cart.items.values.toList()[i].id,
              cart.items.keys.toList()[i],
              cart.items.values.toList()[i].price,
              cart.items.values.toList()[i].quantity,
              cart.items.values.toList()[i].name)),
      FlatButton(
          onPressed: () {},
          child: Text(
            'CHECKOUT',
            style: TextStyle(color: Color(0xFFD1845A), fontSize: 18.0),
          ))
    ],
  ),
);

} }

为什么会出现错误“位置参数过多:期望0,但是找到5。请尝试删除多余的位置参数,或为命名参数指定名称”。有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

CartPdt期望使用命名参数(在CartPdt类中定义,参数用大括号{}包围)。我假设这些名称类似于id, key, price, quantity, name

将您对CartPdt类的实例更新为此

CartPdt(
    id: cart.items.values.toList()[i].id,
    key: cart.items.keys.toList()[i],
    price: cart.items.values.toList()[i].price,
    quantity: cart.items.values.toList()[i].quantity,
    name: cart.items.values.toList()[i].name))

答案 1 :(得分:0)

这是我的CartPdt代码...

''' 导入'package:flutter / material.dart';

class CartPdt extends StatelessWidget {
 final String id;
 final String productId;
 final double price;
 final int quantity;
 final String name;

 CartPdt ({this.id, this.productId, this.price, this.quantity, this.name});

 @override
 Widget build(BuildContext context) {
  return Card(
   child: ListTile(
    leading: CircleAvatar(
      child: FittedBox(
        child: Text('Php$price'),
      ),
    ),
    title: Text(name),
    subtitle: Text('Total: Php${(price * quantity)}'),
    trailing: Text('$quantity x'),
  ),
);

} } '''