This an medicine application. Everything is working fine. Firebase is connected with project successfully. Orders made through this app also appearing at Firebase. But when I’m trying to display of current user (loged-in) order history at his profile page it is not appearing.
This is profile page code:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:login/login_page.dart';
class ProfilePage extends StatefulWidget {
const ProfilePage({super.key});
@override
State<ProfilePage> createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
FirebaseFirestore db = FirebaseFirestore.instance;
FirebaseAuth auth = FirebaseAuth.instance;
Stream<QuerySnapshot<Map<String, dynamic>>>? dataStream;
User? currentUser;
@override
void initState() {
super.initState();
currentUser = auth.currentUser;
if (currentUser != null) {
dataStream = db
.collection('orders')
.where('user_id', isEqualTo: currentUser!.uid)
.snapshots();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: const Text(
'Profile',
style: TextStyle(color: Colors.white),
),
),
body: Center(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: double.infinity,
child: currentUser == null
? Column(
children: [
Center(
child: Text(
'You are currently not logged in!',
style: TextStyle(fontSize: 18),
),
),
SizedBox(
height: 10,
),
ElevatedButton(
onPressed: () async {
await Navigator.of(context).push(MaterialPageRoute(
builder: (context) => LoginPage(),
));
setState(() {
currentUser = auth.currentUser;
if (currentUser != null) {
dataStream = db.collection('orders')
.where('user_id', isEqualTo: currentUser!.uid)
.snapshots();
}
});
},
child: const Text('Login'),
),
],
)
: Column(
children: [
const SizedBox(
height: 32,
),
// Lottie.asset('assets/icons/profileAnimation2.json'),
CircleAvatar(
radius: 70,
backgroundImage: AssetImage('assets/icons/person.png'),
),
SizedBox(
height: 10,
),
Text(
currentUser!.email!,
style: TextStyle(fontSize: 17),
),
SizedBox(
height: 23,
),
SizedBox(
width: 120,
child: ElevatedButton(
onPressed: () {
auth.signOut().then((value) {
setState(() {
currentUser = null;
dataStream = null;
});
});
},
child: Text('Logout'),
),
),
SizedBox(
height: 23,
),
Text(
'My Orders',
style: TextStyle(fontSize: 17),
),
if (currentUser != null)
StreamBuilder(
stream: dataStream,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator.adaptive();
}
if (snapshot.hasError) {
return Text(snapshot.error.toString());
}
if (!snapshot.hasData) {
return Text('No data available');
}
if (snapshot.data == null || snapshot.data!.docs.isEmpty){
return Text('No previous orders');
}
return ListView.builder(
itemCount: snapshot.data!.docs.length,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemBuilder: (context, index) {
DocumentSnapshot order = snapshot.data!.docs[index];
return Card(
child: ListTile(
title: Text(order.id),
subtitle: Text(order.data().toString()),
),
);
},
);
},
),
],
),
),
),
),
),
);
}
}
It is displaying “No previous orders”. Although the user has made orders and his orders are available at firebase using FirebaseFirestore.