How can I retrieve data from firebase without refresh in flutter

141

An easy way to handle your issue is to use "cloud_firestore" package. i'll make u a sample code below..

Note : Before using Firestore, you must first have ensured you have initialized FlutterFire! follow this link: https://firebase.flutter.dev/docs/overview/#initializing-flutterfire

import 'package:flutter/material.dart';


class TestWidget extends StatefulWidget {
  const TestWidget({Key? key}) : super(key: key);

  @override
  _TestWidgetState createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {
  final Stream<QuerySnapshot> _usersStream =
      Firebas`eFirestore.instance.collection('your collection name in database here').snapshots();`

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: _usersStream,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) {
          return const Text('Something went wrong');
        }

    if (snapshot.connectionState == ConnectionState.waiting) {
      return const LinearProgressIndicator();
    }


//this way you can extract your data from DB

//Of course it totally depend on if your data in Db is nested or not. just adjust it with your needs
    return ListView(
      children: snapshot.data!.docs.map((DocumentSnapshot document) {
        Map<String, dynamic> data =
            document.data()! as Map<String, dynamic>;
        return Column(children: [
          PassDataToAnotherWidget(data),
        ]);
      }).toList(),
    );
  },
);
  }
}
Share:
141
Frank van Puffelen
Author by

Frank van Puffelen

I am an engineer for Firebase at Google. I respond equally well to being called "Frank" or "puf".

Updated on January 04, 2023

Comments

  • Frank van Puffelen
    Frank van Puffelen over 1 year

    I have Boolean value in Firebase Realtime database i want a updated value without a screen Refresh in flutter. i want it to be updated streamline on flutter

  • Frank van Puffelen
    Frank van Puffelen about 2 years
    OP mentioned "Firebase Realtime database" in their question, which is a different database. The approach with a StreamBuilder applies to the Realtime Database too, but its API will be different.