提问人:Tony 提问时间:10/24/2023 最后编辑:Frank van PuffelenTony 更新时间:10/28/2023 访问量:22
检索购物车中的商品
Retrieving items in cart
问:
我正在尝试从 firebase 检索用户购物车中的项目。它的格式是这样的:1231352385:2。正在检索计数。getitemsquantity()
我需要使用 whereIn 在 firebase 查询中使用,但它是异步的 - 所以我不能在查询中使用 await。
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:ionicons/ionicons.dart';
import 'package:order_sage_user/Services/firebaseservices.dart';
import 'package:order_sage_user/models/sellers.dart';
import 'package:order_sage_user/models/sellersitems.dart';
import 'package:provider/provider.dart';
import '../Services/sharedpreferences.dart';
import '../models/cart_item_counter.dart';
import '../widgets/cart_items_designs.dart';
class CartScreen extends StatefulWidget {
final String? selleruid;
CartScreen({this.selleruid, Key? key}) : super(key: key);
@override
State<CartScreen> createState() => _CartScreenState();
}
class _CartScreenState extends State<CartScreen> {
late User _user;
late double _deviceHeight, _deviceWidth;
late MySharedPreferences _sharedPreferences;
late FireBaseService _fireBaseService;
List<int> separateItemQuantityList = [];
getItemsQuantities() async {
List<String> defaultItemList = [];
int i = 0;
defaultItemList = await _fireBaseService.getUserCart();
for (i; i < defaultItemList.length; i++) {
// this is a list so we used this function to get each item in the list
String item = defaultItemList[i].toString();
List<String> listItemCharacters = item.split(':').toList();
//this is to get the number which is the first index after the : split and assign it to the quantity number
var quantityNumber = int.parse(listItemCharacters[1].toString());
print('\nthis is quantity Number now = ' + quantityNumber.toString());
separateItemQuantityList.add(quantityNumber);
}
print('\nthis is Quantity list now = ');
print(separateItemQuantityList);
}
@override
void initState() {
super.initState();
_sharedPreferences = MySharedPreferences();
_fireBaseService = GetIt.instance.get<FireBaseService>();
_user = FirebaseAuth.instance.currentUser!;
getItemsQuantities();
}
@override
Widget build(BuildContext context) {
_deviceHeight = MediaQuery.of(context).size.height;
_deviceWidth = MediaQuery.of(context).size.width;
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => CartItemCounter(_sharedPreferences),
)
],
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(
Ionicons.chevron_back,
color: Color(0xFF76966D),
),
),
actions: [
Stack(
children: [
IconButton(
onPressed: () {},
icon: Icon(
Ionicons.cart,
size: 30,
color: Color(0xFF76966D),
),
),
Positioned(
top: 8,
right: 8,
child: Container(
padding: EdgeInsets.all(4),
decoration: BoxDecoration(
color: Color(0xFF76966D), shape: BoxShape.circle),
child: Consumer<CartItemCounter>(
builder: (context, counter, child) {
return Text(
counter.count.toString(),
style: TextStyle(color: Colors.white),
);
},
),
),
)
],
),
],
),
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Align(
alignment: Alignment.bottomLeft,
child: FloatingActionButton.extended(
onPressed: () {},
label: Text('Clear Cart'),
icon: Icon(Icons.clear_all),
backgroundColor: Colors.blue,
),
),
Align(
alignment: Alignment.bottomLeft,
child: FloatingActionButton.extended(
onPressed: () {},
label: Text('Checkout'),
icon: Icon(Icons.navigate_next),
backgroundColor: Colors.blue,
),
),
],
),
body: CustomScrollView(
slivers: [
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('items')
.where('itemId', whereIn: getItemsQuantities())
.orderBy('itemtime', descending: true)
.snapshots(),
builder: (context, snapshot) {
return !snapshot.hasData
? SliverToBoxAdapter(
child: Center(
child: CircularProgressIndicator(),
),
)
: snapshot.data!.docs.isEmpty
? Container()
: SliverList(delegate:
SliverChildBuilderDelegate((context, index) {
Itemsmodel model = Itemsmodel.fromJson(
snapshot.data!.docs[index].data()!
as Map<String, dynamic>);
return CartItemDesign(
model: model,
context: context,
itemsQuantityList: [index],
);
}));
},
)
],
),
),
);
}
}
我试图在 StreamBuilder 之上使用 FutureBuilder,但这也没有用
答: 暂无答案
评论