I have a quick question. "snapshot.hasError" condition is getting true and "snapshot.hasData" condition is false. Output that I am getting on print in "snapshot.hasError" is "Invalid argument(s)". What does it mean?
Stream is getting data from database using "databaseServices" class but snapshot is not retrieving any data and it has error
class CustProfile extends StatefulWidget {
@override
_CustProfileState createState() => _CustProfileState();
}
class _CustProfileState extends State<CustProfile> {
final AuthService _auth = AuthService();
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return StreamBuilder<CustData>(
stream: user != null ? DatabaseService(uid: user.uid).getCustData : null,
builder: (context, snapshot) {
debugPrint("User: " + user.toString());
// <<<<<< ---- Below condition is TRUE ---- >>>>>>
if (snapshot.hasError) {
print("Snapshot error: " + snapshot.error.toString());
}
if (snapshot.hasData) {
// ... Here is the widget which simply display the snapshot data but
}
}
Below is the DataServices.dart class that sends the data to database and retrieve data from database
class DatabaseService {
// collection reference is just reference for certain collection
final CollectionReference custCollection =
Firestore.instance.collection('cust');
final String uid;
DatabaseService({this.uid});
// Cust data from snapshot
CustData _custDataFromSnapshot(DocumentSnapshot snapshot) {
print(" UiD DB TEST" + uid + " USerNAme: " + snapshot.data['username']);
return CustData(
custId: uid,
custPhNo: snapshot.data['custPhNo'] ?? "",
custName: snapshot.data['custName'] ?? "",
custDateOfBirth: (snapshot.data['custDateOfBirth'] as Timestamp).toDate() ?? "",
custAddDate: (snapshot.data['custDateOfBirth'] as Timestamp).toDate() ?? "",
);
}
// Get user doc stream
Stream<CustData> get getCustData {
return custCollection.document(uid).snapshots().map(_custDataFromSnapshot);
}
// ------------------------------- UPDATION AND RETRIVAL OF CUSTOMER DATA
Future updateCustData(Map<String, dynamic> dataMap) async {
// - Setting ID first in a document
await custCollection.document(uid).setData(
{
'custID': uid,
'custAddDate': DateTime.now(),
},
merge: true,
);
// - Dynamically adding data in the db
dataMap.forEach(
(key, value) async {
await custCollection.document(uid).setData(
{
key: value,
},
merge: true,
);
},
);
}
Below is the User.dart class
class User {
final String uid;
User({this.uid});
}
class CustData {
String custId;
String custName;
String custPhNo;
DateTime custDateOfBirth;
DateTime custAddDate;
CustData({
this.custId,
this.custName,
this.custPhNo,
this.custDateOfBirth,
this.custAddDate,
});
}
Below is the output
I/flutter (21627): User: Instance of 'User'
I/flutter (21627): Snapshot error: Invalid argument(s)