I'm trying to retrieve the items in the usercart from firebase. It's in this format :1231352385:2. The getitemsquantity() is retrieving the count.
I need to use in the firebase query using whereIn, but its async - so i can't use await in the query.
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],
);
}));
},
)
],
),
),
);
}
}
ive tried to use a futurebuilder above the streambuilder but that also did not work