{"id":10202,"date":"2024-04-26T05:46:09","date_gmt":"2024-04-26T05:46:09","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=10202"},"modified":"2024-04-26T05:46:09","modified_gmt":"2024-04-26T05:46:09","slug":"get-data-from-firebase-in-flutter","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/flutter\/get-data-from-firebase-in-flutter","title":{"rendered":"How to Get Data from Firebase in Flutter"},"content":{"rendered":"<p>Here&#8217;s a basic example of how to get data from Firestore Database:<\/p>\n<h3>Firestore database :<\/h3>\n<p><img decoding=\"async\" class=\"alignnone size-large wp-image-10207\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/04\/26054318\/Screenshot-2024-04-17-at-3.17.37%E2%80%AFPM-1024x491.png\" alt=\"Firestore database \" width=\"1024\" height=\"491\" srcset=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/04\/26054318\/Screenshot-2024-04-17-at-3.17.37%E2%80%AFPM-1024x491.png 1024w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/04\/26054318\/Screenshot-2024-04-17-at-3.17.37%E2%80%AFPM-300x144.png 300w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/04\/26054318\/Screenshot-2024-04-17-at-3.17.37%E2%80%AFPM-768x368.png 768w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/04\/26054318\/Screenshot-2024-04-17-at-3.17.37%E2%80%AFPM-1536x737.png 1536w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/04\/26054318\/Screenshot-2024-04-17-at-3.17.37%E2%80%AFPM.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h3>To get data only one-time :<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">\r\nFutureBuilder&lt;QuerySnapshot&gt;(\r\n future: FirebaseFirestore.instance.collection('Users').get(),\r\n builder:\r\n     (BuildContext context, AsyncSnapshot&lt;QuerySnapshot&gt; snapshot) {\r\n   if (snapshot.hasError) {\r\n     return const Text('Something went wrong');\r\n   }\r\n\r\n   if (snapshot.connectionState == ConnectionState.waiting) {\r\n     return const Text(\"Loading\");\r\n   }\r\n\r\n   return ListView.builder(\r\n     itemCount: snapshot.data?.docs.length,\r\n     itemBuilder: (context, index) {\r\n       final data = snapshot.data?.docs[index];\r\n       return ListTile(\r\n         title: Text(data?['name'] ?? ''),\r\n         subtitle: Text(data?['email'] ?? ''),\r\n         trailing: Text(data?['dob'] ?? ''),\r\n       );\r\n     },\r\n   );\r\n },\r\n)<\/pre>\n<h3>To get real time data : <\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"groovy\">\r\nStreamBuilder<QuerySnapshot>(\r\n stream: FirebaseFirestore.instance.collection('Users').snapshots(),\r\n builder:\r\n     (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {\r\n   if (snapshot.hasError) {\r\n     return const Text('Something went wrong');\r\n   }\r\n\r\n   if (snapshot.connectionState == ConnectionState.waiting) {\r\n     return const Text(\"Loading\");\r\n   }\r\n\r\n   return ListView.builder(\r\n     itemCount: snapshot.data?.docs.length,\r\n     itemBuilder: (context, index) {\r\n       final data = snapshot.data?.docs[index];\r\n       return ListTile(\r\n         title: Text(data?['name'] ?? ''),\r\n         subtitle: Text(data?['email'] ?? ''),\r\n         trailing: Text(data?['dob'] ?? ''),\r\n       );\r\n     },\r\n   );\r\n },\r\n)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a basic example of how to get data from Firestore Database: Firestore database : To get data only one-time : FutureBuilder&lt;QuerySnapshot&gt;( future: FirebaseFirestore.instance.collection(&#8216;Users&#8217;).get(), builder: (BuildContext context, AsyncSnapshot&lt;QuerySnapshot&gt; snapshot) { if (snapshot.hasError) { return const Text(&#8216;Something went wrong&#8217;); } if (snapshot.connectionState == ConnectionState.waiting) { return const Text(&#8220;Loading&#8221;); } return ListView.builder( itemCount: snapshot.data?.docs.length, itemBuilder: (context, index) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10204,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[15],"tags":[],"class_list":["post-10202","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flutter"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10202"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=10202"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10202\/revisions"}],"predecessor-version":[{"id":10209,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10202\/revisions\/10209"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/10204"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=10202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=10202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=10202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}