0

I am getting error at this point. The result works fine but it shows error for a second. I think this is because i'm using FutureBuilder inside a FutureBuilder. I need to call two methods at 'future:' so instead of that i used another FutureBuilder but it is showing error.

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;
  
}

2 Answers2

0

You can use FutureBuilder inside FutureBuilder without any problem. You are getting error because you are trying to access the data before it is ready. Try this.

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();
    },
  );
bcihan
  • 331
  • 2
  • 9
0

I solved it by this

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