我有一个简短的问题。 “ snapshot.hasError”条件为真,而“ snapshot.hasData”条件为假。我在“ snapshot.hasError”中打印的输出是“无效参数”。什么意思?
Stream正在使用“ databaseServices”类从数据库获取数据,但是快照未检索到任何数据,并且有错误
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
}
}
下面是DataServices.dart类,该类将数据发送到数据库并从数据库中检索数据
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,
);
},
);
}
下面是User.dart类
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,
});
}
下面是输出
I/flutter (21627): User: Instance of 'User'
I/flutter (21627): Snapshot error: Invalid argument(s)
答案 0 :(得分:1)
遇到同样的问题,请参阅此帖子:StreamBuilder throws Dirty State saying Invalid Arguments
您的打印语句是导致出现错误的行,特别是以下行:
print(" UiD DB TEST" + uid + " USerNAme: " + snapshot.data['username']);