0

So here I am trying to fetch the data from firebase by using DocumentSnapshot. I want to print the value of document['display_name']

  • Collection = user_data
  • document = 3vIf92LIJQ7pu7MpUwH1
  • display_name = element of document.

Output: 'Error has Occured' on screen

     class _HomeViewState extends State<HomeView> {
         Future<DocumentSnapshot> getDocument() async {
             return Firestore.instance
            .collection('user_data')
            .document('3vIf92LIJQ7pu7MpUwH1')
            .get();
          }

     @override
        Widget build(BuildContext context) {

           return Container(
           child: Center(
           child: FutureBuilder(
           future: getDocument(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
      if (snapshot.hasError) return Text('Error has occured');
      if (snapshot.connectionState == ConnectionState.waiting) {
        return CircularProgressIndicator();
      }
      if (snapshot.hasData) {
        return Column(
          children: <Widget>[
            Text(snapshot.data['display_name']),
          ],
        );
      }
Harsh
  • 83
  • 2
  • 7
  • Have you checked to security rules of firestore? – dshukertjr Feb 09 '20 at 00:46
  • yes changing security rules of firestore made it work. – Harsh Feb 09 '20 at 10:50
  • The things I changed was:Go in Database -> Rules -> Change allow read, write: if false; to true; (I dont think this is the best solution for this as it removes the entire security of google authentication). Any alternate solution would be helpful. @dshukertjr. – Harsh Feb 09 '20 at 10:56

2 Answers2

0

If hasError is true, you'll want to print snapshot.error to see what the actual problem is:

if (snapshot.hasError) return Text('Error has occurred: ${snapshot.error}');
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Error performing get, Permission_denied: missing or insuuficient permission was the error, so I just change the security rule and it worked. – Harsh Feb 09 '20 at 10:51
  • can you find the solution for this issue?link as follows [link](https://stackoverflow.com/questions/60142173/not-able-to-access-the-data-from-firebase-using-streambuilder-in-flutter?noredirect=1#comment106385199_60142173) – Harsh Feb 10 '20 at 15:55
  • I did upvote for your answer at the begining itself because it did helped me but I thinks its the rules of stackoverflow to show that. Following message I recieved after giving an upvote to your answers or anyone else answer : **Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.** – Harsh Feb 10 '20 at 19:04
0

You probably have an error because of your firestore security rules. Also, you will need to access snapshot.data.data['display_name'] instread of snapshot.data['display_name'] to get the value from firestore.

 if (snapshot.hasData) {
    return Column(
      children: <Widget>[
        Text(snapshot.data.data['display_name']),
      ],
    );
  }
dshukertjr
  • 15,244
  • 11
  • 57
  • 94
  • 1
    I changed my database security rules and it did worked for me. – Harsh Feb 09 '20 at 10:49
  • can you find the solution for this issue?link as follows [link](https://stackoverflow.com/questions/60142173/not-able-to-access-the-data-from-firebase-using-streambuilder-in-flutter?noredirect=1#comment106385199_60142173) – Harsh Feb 10 '20 at 15:56
  • @Harsh I will take a look at it! – dshukertjr Feb 11 '20 at 00:42