查询快照为空

时间:2020-07-27 20:38:08

标签: firebase flutter google-cloud-firestore

我可以在流查询中得到null返回。有趣的是,数据通过了,但是在处理它以在应用程序中使用它时,它在某个地方丢失了。我知道我可能在某个地方犯了一个愚蠢的错误,但是我已经研究了三天了。请帮忙。

这是流

  Stream <SellerProfile> get sellerProfile {
    return  sellerProfileCollection.document(uid).snapshots()
        .map(yieldSellerProfile);
  }

  SellerProfile yieldSellerProfile(DocumentSnapshot snapshot) {
    print(snapshot.data['shopName']);
    return SellerProfile(
      shopName: snapshot.data['shopName'] ?? '',
      phone: snapshot.data['phone']??'',
      credit: snapshot.data['credit'] ?? '',
      posts: snapshot.data['posts'] ?? '',
      sales: snapshot.data['sales'] ?? '',
      avatarUrl: snapshot.data['avatarUrl'] ?? '',
      location:snapshot.data['location'] ?? '',
      rating: snapshot.data['rating'] ?? '',
      joinedDate: snapshot.data['joinedDate'] ?? '',
    );
  }

我的想法是,在查询流SellerProfile流之后,我想将其映射到自定义模型中以在应用程序中使用它。

如代码中所示,我在处理之前先打印snapshot.data['shopName']并得到输出

I/flutter ( 1008): Soko

这意味着数据来自Firestore,但是当我尝试访问前端的数据时,我会收到一个空值

这是前端

  Widget build(BuildContext context) {

    final AuthService _auth = AuthService();
    final user = Provider.of<User>(context);

    return StreamBuilder<SellerProfile>(

      stream: SellerDatabaseService(uid: user.uid).sellerProfile,
      builder: (context, snapshot) {
        SellerProfile profile=snapshot.data;
        print(profile);
        return Scaffold(
          backgroundColor: Colors.white,
          appBar: header(context,strTitle: "Profile"),
          body: SingleChildScrollView(),
        );
      }
    );
  }

这是我尝试打印配置文件时的输出

I/flutter ( 1008): null

我要去哪里错了?预先感谢!

2 个答案:

答案 0 :(得分:1)

我去发行。我试图用流构建器构建流,而不是从提供程序返回流。

所以我改变了这个...

/**
 * General decryption routine for recovering a plaintext
 * @param String $string the cypher text to decrypt
 * @param String $key the encryption key to use
 * @return String the plain text result
 */
function decrypt($string, $key)
{
    $ptxt = base64_decode($string);
    /* Open module, and create IV */
    $td = mcrypt_module_open('des', '', 'cfb', '');
    $ksub = substr(md5($key), 0, mcrypt_enc_get_key_size($td));
    $iv_size = mcrypt_enc_get_iv_size($td);
    $iv = substr($ptxt, 0, $iv_size);
    $ptxtsub = substr($ptxt, $iv_size);
    /* Initialize encryption handle */
    if (mcrypt_generic_init($td, $ksub, $iv) != -1)
    {
        /* Encrypt data */
        $ctxt = mdecrypt_generic($td, $ptxtsub);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $ctxt;
    } //end if
}

对此...

  Widget build(BuildContext context) {

    final AuthService _auth = AuthService();
    final user = Provider.of<User>(context);

    return StreamBuilder<SellerProfile>(

      stream: SellerDatabaseService(uid: user.uid).sellerProfile,
      builder: (context, snapshot) {
        SellerProfile profile=snapshot.data;
        print(profile);
        return Scaffold(
          backgroundColor: Colors.white,
          appBar: header(context,strTitle: "Profile"),
          body: SingleChildScrollView(),
        );
      }
    );
  }

答案 1 :(得分:0)

您不检查快照是否有数据。使用hasData的{​​{1}}属性为此添加检查:

AsyncSnapshot

理想情况下,您还应该检查它是否return StreamBuilder<SellerProfile>( stream: SellerDatabaseService(uid: user.uid).sellerProfile, builder: (context, snapshot) { if(snapshot.hasError) { return Text(snapshot.error.toString()); } if(!snapshot.hasData) {//Check if the snapshot actually has data return CircularProgressIndicator(); } SellerProfile profile=snapshot.data; print(profile); return Scaffold( backgroundColor: Colors.white, appBar: header(context,strTitle: "Profile"), body: SingleChildScrollView(), ); } ); ,如果您想更精细地控制显示内容,可以使用hasError

相关问题