如何解决Flutter Provider setState() 或 markNeedsBuild() 在构建过程中调用:Provider
我想计算购物车的总价。我正在为此尝试提供程序,但它显示问题 当我在购物车中添加商品时,总价显示正确,但是当我删除其中一个购物车时,总价先增加,然后我返回上一页,再次转到购物车页面,然后总价降低。
**cart page code **
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ecommerce_app/Model/AppData.dart';
import 'package:flutter_ecommerce_app/screens/checkout.dart';
import 'package:provider/provider.dart';
import 'package:firebase_auth/firebase_auth.dart';
class Cart extends StatefulWidget {
@override
_CartState createState() => _CartState();
}
class _CartState extends State<Cart> {
double totalPrice=0.0;
double grandTotalPrice = 0.0;
double shipPrice =50.0;
final CollectionReference _userRef = FirebaseFirestore.instance.collection("users");
final CollectionReference _productsRef = FirebaseFirestore.instance.collection("products");
User _user = FirebaseAuth.instance.currentUser;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
elevation: 0.1,backgroundColor: Colors.red,title: Text('Cart'),actions: [
Padding(
padding: const EdgeInsets.fromLTRB(0,10,0),child: GestureDetector(
onTap: (){
},child: Container(
width: 54.0,height: 44.0,decoration: BoxDecoration(
color: Colors.redAccent,borderRadius: BorderRadius.circular(8.0),),alignment: Alignment.center,child: StreamBuilder(
stream: _userRef.doc(_user.uid).collection("carts").snapshots(),builder: (context,snapshot){
int _totalItems = 0 ;
if(snapshot.connectionState == ConnectionState.active) {
List _documents = snapshot.data.docs;
_totalItems = _documents.length;
}
return Text(
"$_totalItems" ?? "0",style: TextStyle(
fontSize: 28.0,fontWeight: FontWeight.w600,color: Colors.white,);
}
),],body: Consumer<AppData>(
builder: (context,value,child) => Stack(
children: [
FutureBuilder<QuerySnapshot>(
future: _userRef.doc(_user.uid).collection("carts").get(),snapshot) {
if (snapshot.hasError) {
return Scaffold(
body: Center(
child: Text("Error: ${snapshot.error}"),);
}
// Collection Data ready to display
if (snapshot.connectionState == ConnectionState.done) {
// Display the data inside a list view
return ListView(
padding: EdgeInsets.only(
top: 10.0,bottom: 12.0,children: snapshot.data.docs.map((document)
{
return GestureDetector(
onTap: () {
},child: FutureBuilder(
future: _productsRef.doc(document.id).get(),productSnap,) {
if (productSnap.hasError) {
return Container(
child: Center(
child: Text("${productSnap.error}"),);
}
if (productSnap.connectionState == ConnectionState
.done) {
//total price calculation
if (productSnap.hasData) {
Map _productMap = productSnap.data.data();
totalPrice = totalPrice + document.data()["quantity"] * _productMap['price'];
grandTotalPrice = shipPrice + totalPrice ;
Provider.of<AppData>(context,listen: false).calculateTotalPrice(grandTotalPrice);
return InkWell(
onTap: () {
Navigator.push(context,MaterialPageRoute(builder: (context)=>Checkout()));
},child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 16.0,horizontal: 24.0,child: Row(
mainAxisAlignment:
MainAxisAlignment.start,children: [
Container(
width: 100,height: 100,child: ClipRRect(
borderRadius:
BorderRadius.circular(8.0),child: Image.network(
"${_productMap['images'][0]}",fit: BoxFit.cover,Container(
padding: EdgeInsets.only(left: 16.0,child: Column(
mainAxisAlignment:
MainAxisAlignment.start,crossAxisAlignment:
CrossAxisAlignment.start,children: [
Text(
"${_productMap['name']}",style: TextStyle(
fontSize: 18.0,color: Colors.black,fontWeight:
FontWeight.bold),Padding(
padding: const EdgeInsets
.symmetric(
vertical: 4.0,child: Text(
"\€${_productMap['price']}",style: TextStyle(
fontSize: 16.0,color: Colors.red,fontWeight:
FontWeight.bold),child: Text(
"Size -${document.data()['size']}",fontWeight:
FontWeight.w600),child: Text(
"quantity -${document.data()['quantity']}",Padding(
padding: const EdgeInsets.all(8.0),child: new IconButton(icon: Icon(Icons.delete),onPressed: () async {
grandTotalPrice = grandTotalPrice - document.data()["quantity"] * _productMap['price'];
if(grandTotalPrice<=50.0){
grandTotalPrice=0.0;
}
await Provider.of<AppData>(context,listen: false).deleteData(grandTotalPrice,document.id) ;
}),);
}
}
return Container(
child: Center(
child: CircularProgressIndicator(),);
},);
}).toList(),);
}
// Loading State
return Scaffold(
body: Center(
child: CircularProgressIndicator(),);
},bottomNavigationBar:Consumer<AppData>(
builder: (context,appData,child ) => Stack(
children: [
Container(
color: Colors.white,child: Row(
children: <Widget>[
Expanded(child: ListTile(
title: new Text("Shipping Tax:"),subtitle: new Text("\€${Provider.of<AppData>(context,listen: false).shipmentPrice.toString()}"),)),Expanded(child: ListTile(
title:new MaterialButton(onPressed: () {
showCustomDialog();
},textColor: Colors.white,elevation: 0.2,child: new Text("Total"),Expanded(child: Padding(
padding: const EdgeInsets.fromLTRB(0,20,child: new MaterialButton(onPressed: (){
Navigator.push(context,MaterialPageRoute(builder: (context)=>Checkout()));
},child: new Text("Checkout",style: TextStyle(color: Colors.white),);
}
void showCustomDialog() 异步{
AlertDialog alertDialog = AlertDialog(
title: Padding(
padding: const EdgeInsets.all(8.0),child: Text("Total Price = \€${Provider.of<AppData>(context,listen: false).price.toString()} "),// child: Text("Total Price = "+ grandTotalPrice.toString()),);
await showDialog(context: context,builder: (context){
return alertDialog;
});
}
}
***appdata provider code***
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
class AppData extends ChangeNotifier {
deleteData(double grandTotalPrice,String id) async{
final CollectionReference _userRef = FirebaseFirestore.instance.collection("users");
User _user = FirebaseAuth.instance.currentUser;
final CollectionReference collectionReference =_userRef.doc(_user.uid).collection("carts");
await collectionReference.doc(id).delete();
notifyListeners();
calculateTotalPrice(grandTotalPrice);
notifyListeners();
}
double price = 0.0;
double shipmentPrice = 50.0;
calculateTotalPrice(double grandTotalPrice) {
price = grandTotalPrice;
notifyListeners();
} }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。