What is the problem of this code?
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/cart.dart' show Cart;
import '../widgets/cart_item.dart';
import '../providers/orders.dart';
class CartScreen extends StatelessWidget {
static const routeName = '/cart';
@override
Widget build(BuildContext context) {
final cart = Provider.of<Cart>(context);
return Scaffold(
appBar: AppBar(
title: Text('Your Cart'),
),
body: Column(
children: <Widget>[
Card(
margin: EdgeInsets.all(15),
child: Padding(
padding: EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
const Text(
'Total',
style: TextStyle(fontSize: 20),
),
const Spacer(),
Chip(
label: Text(
'\$${cart.totalAmount.toStringAsFixed(2)}',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
),
backgroundColor: Theme.of(context).colorScheme.primary,
),
FlatButton(
child: Text('ORDER NOW'),
onPressed: () {
Provider.of<Orders>(context, listen: false).addOrder(
cart.items.values.toList(),
cart.totalAmount,
);
cart.clear();
},
textColor: Theme.of(context).colorScheme.primary,
)
],
),
),
),
SizedBox(height: 10),
Expanded(
child: ListView.builder(
itemCount: cart.items.length,
itemBuilder: (ctx, i) => CartItem(
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].title,
),
),
)
],
),
);
}
}
This is what I see inside the DEBUG CONSOLE:
════════ Exception caught by widgets library ═══════════════════════════════════ The following assertion was thrown while applying parent data.: Incorrect use of ParentDataWidget.
The ParentDataWidget Expanded(flex: 1) wants to apply ParentData of type FlexParentData to a RenderObject, which has been set up to accept ParentData of incompatible type ParentData.
Usually, this means that the Expanded widget has the wrong ancestor RenderObjectWidget. Typically, Expanded widgets are placed directly inside Flex widgets. The offending Expanded is currently placed inside a FractionalTranslation widget.
The ownership chain for the RenderObject that received the incompatible parent data was: Semantics ← Card ← Expanded ← FractionalTranslation ← SlideTransition ← Stack ← Listener ← _GestureSemantics ← RawGestureDetector ← GestureDetector ← ⋯ When the exception was thrown, this was the stack
EDIT: I think maybe the error happens at this code:
class CartItem extends StatelessWidget {
final String id;
final String productId;
final double price;
final int quantity;
final String title;
CartItem(
this.id,
this.productId,
this.price,
this.quantity,
this.title,
);
@override
Widget build(BuildContext context) {
return Dismissible(
key: ValueKey(id),
background: Container(
// color: Theme.of(context).colorScheme.primary,
child: Icon(
Icons.delete,
color: Colors.white,
size: 40,
),
alignment: Alignment.centerRight,
padding: EdgeInsets.only(right: 20),
margin: EdgeInsets.symmetric(
horizontal: 15,
vertical: 4,
),
),
direction: DismissDirection.endToStart,
onDismissed: (direction) {
Provider.of<Cart>(context, listen: false).removeItem(productId);
},
child: Expanded(
child: Card(
margin: EdgeInsets.symmetric(
horizontal: 15,
vertical: 4,
),
child: Padding(
padding: EdgeInsets.all(8),
child: ListTile(
leading: CircleAvatar(
radius: 30,
backgroundColor: Theme.of(context).colorScheme.primary,
child: Padding(
padding: EdgeInsets.all(5),
child: FittedBox(
child: Text(
'\$$price',
style: TextStyle(fontWeight: FontWeight.w900),
),
),
),
),
title: Text(
title,
style: TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text('Total: \$${(price * quantity)}'),
trailing: Text('$quantity x'),
),
),
),
),
);
}
}
cartItem.dartcode to the EDIT part of my question.Expandedis a child ofDismissiblein there.