littleironical
littleironical

Reputation: 1914

How to get a single data from Firebase without using FutureBuilder or StreamBuilder in Flutter

I want to retrieve a single data or field from the Firestore. Like firstName, lastName or title of user1 in this case

enter image description here

I've gone through the FlutterFire documentation, it was using FutureBuilder to read data from Firestore. I searched about this on Stack Overflow also but didn't get any perfect answer for that.

Upvotes: 3

Views: 2183

Answers (2)

Rajan Vishwakarma
Rajan Vishwakarma

Reputation: 41

Just reframing the above solution a bit for those who didn't find exactly what they are looking for (get data of all the documents of a collection)

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class Screen extends StatefulWidget{
  final String documentId;
  Screen({this.documentId});

  @override
  _ScreenState createState() => _ScreenState();
}
class _ScreenState extends State<Screen>{
  String itemName = '';
  String itemImage = '';  //Initializing string bcz image requires a path
  String itemQuantity = '';
  CollectionReference collectionReference =
       FirebaseFirestore.instance.collection('CollectionName');
  @override
  void initState(){
    //documentId is passed from previous widget
    collectionReference.doc(widget.documentId).get().then((value) {
      //'value' is the instance of 'DocumentSnapshot'
      //'value.data()' contains all the data inside a document in the form of 'dictionary'
      setState(() {
        //name, image, quantity are the fields of document
        itemName = value.data()['name']; 
        itemImage = value.data()['image'];
        itemQuantity = value.data()['quantity'].toString(); //quantity field is of type number in firebase
        //while retrieving numerical value from firebase, convert it to string before displaying
      });
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
  return Scaffold(
    body: Column(
      children: [
        Image.network(itemImage),
        Text(itemName),
        Text(itemQuantity),
      ]
    ));
  }
}

Upvotes: 0

littleironical
littleironical

Reputation: 1914

Answering my own question as I didn't find any perfect answer for this question.

//Initialising as 'loading..' because text widget (where you'll be using these variables) can't be null
String firstName = 'loading...'
String lastName = 'loading...'
String title = 'loading...'

class Screen extends StatefulWidget {
  @override
  _ScreenState createState() => _ScreenState();
}

class _ScreenState extends State<Screen> {

  //Creating a reference to the collection 'users'
  CollectionReference collectionReference =
    FirebaseFirestore.instance.collection('users');

  //This function will set the values to firstName, lastName and title from the data fetched from firestore 
  void getUsersData() {
    collectionReferenceToOrderacWeb.doc('user1').get().then((value) {

      //'value' is the instance of 'DocumentSnapshot'
      //'value.data()' contains all the data inside a document in the form of 'dictionary'
      var fields = value.data();

      //Using 'setState' to update the user's data inside the app
      //firstName, lastName and title are 'initialised variables'
      setState(() {
        firstName = fields['firstName'];
        lastName = fields['lastName'];
        title = fields['title'];
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

Upvotes: 6

Related Questions