繁体   English   中英

StreamBuilder 快照上的“无效参数”错误 - Flutter

[英]“Invalid argument(s)” error on snapshot of StreamBuilder - Flutter

我有一个快速的问题。 “snapshot.hasError”条件为真,“snapshot.hasData”条件为假。 我在“snapshot.hasError”中打印的 Output 是“无效参数”。 这是什么意思?

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.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,
  });
}

下面是output

I/flutter (21627): User: Instance of 'User'
I/flutter (21627): Snapshot error: Invalid argument(s)

遇到同样的问题,请参考这篇文章: StreamBuilder throws Dirty State say Invalid Arguments

您的打印语句是导致您遇到错误的行,特别是这一行:

print(" UiD DB TEST" + uid + " USerNAme: " + snapshot.data['username']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM