0

我在这一点上遇到错误。结果工作正常,但它显示错误一秒钟。我认为这是因为我在 FutureBuilder 中使用 FutureBuilder。我需要在“未来:”调用两个方法,所以我使用了另一个 FutureBuilder,但它显示错误。

sendOfferButton() {
    return FutureBuilder(
      initialData: [],
      future: getUserProfile(),
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        cnicCheck = snapshot.data['CNIC'];
        return RaisedButton(
          padding: EdgeInsets.symmetric(vertical: 10),
          child: Text('Send Offer'),
          textColor: Colors.white,
          color: Colors.green,
          onPressed: () {
            if (cnicCheck == "verified") {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => SendOffer(),
                ),
              );
            } else {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => VerifyCNIC(),
                ),
              );
            }
          },
        );
      },
    );
  }
Future getUserProfile() async {
  DocumentSnapshot document = await FirebaseFirestore.instance
      .collection('Users')
      .doc(FirebaseAuth.instance.currentUser.email)
      .get();
      return document;
  
}
4

2 回答 2

0

我通过这个解决了

 Future<String> getCNIC() async {
  DocumentSnapshot document = await FirebaseFirestore.instance
      .collection('Users')
      .doc(FirebaseAuth.instance.currentUser.email)
      .get();
       String getCNIC = document['CNIC'];
      return getCNIC;

}
sendOfferButton() {
    return FutureBuilder<String>(
      initialData: cnicCheck,
      future: getCNIC(),
      builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
        cnicCheck = snapshot.data;
        return RaisedButton(
          padding: EdgeInsets.symmetric(vertical: 10),
          child: Text('Send Offer'),
          textColor: Colors.white,
          color: Colors.green,
          onPressed: () {
            if (cnicCheck == "verified") {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => SendOffer(),
                ),
              );
            } else {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => VerifyCNIC(),
                ),
              );
            }
          },
        );
      },
    );
  }
于 2021-01-03T10:43:27.277 回答
0

您可以在 FutureBuilder 中使用 FutureBuilder 没有任何问题。您收到错误是因为您试图在数据准备好之前访问它。尝试这个。

FutureBuilder(
    future: getUserProfile(),
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        final cnicCheck = snapshot.data['CNIC'];
        return RaisedButton(
          padding: EdgeInsets.symmetric(vertical: 10),
          child: Text('Send Offer'),
          textColor: Colors.white,
          color: Colors.green,
          onPressed: () {
            if (cnicCheck == "verified") {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => SendOffer(),
                ),
              );
            } else {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (_) => VerifyCNIC(),
                ),
              );
            }
          },
        );
      } else
        return CircularProgressIndicator();
    },
  );
于 2021-01-03T08:37:01.723 回答