"Invalid argument(s)" StreamBuilder 快照错误 - Flutter
"Invalid argument(s)" error on snapshot of StreamBuilder - Flutter
我有一个简短的问题。 “snapshot.hasError”条件为真,“snapshot.hasData”条件为假。我在“snapshot.hasError”中打印的输出是“无效参数”。这是什么意思?
Stream 正在使用“databaseServices”从数据库中获取数据class,但快照未检索任何数据并且出现错误
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 将数据发送到数据库并从数据库中检索数据
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.dartclass
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)
遇到同样的问题,请参考这个post:
您的打印语句是导致您收到错误的行,特别是这一行:
print(" UiD DB TEST" + uid + " USerNAme: " + snapshot.data['username']);
我有一个简短的问题。 “snapshot.hasError”条件为真,“snapshot.hasData”条件为假。我在“snapshot.hasError”中打印的输出是“无效参数”。这是什么意思?
Stream 正在使用“databaseServices”从数据库中获取数据class,但快照未检索任何数据并且出现错误
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 将数据发送到数据库并从数据库中检索数据
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.dartclass
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)
遇到同样的问题,请参考这个post:
您的打印语句是导致您收到错误的行,特别是这一行:
print(" UiD DB TEST" + uid + " USerNAme: " + snapshot.data['username']);