我如何从firebase中检索数据,而无需刷新

发布于 2025-01-24 03:37:09 字数 62 浏览 0 评论 0原文

我在Firebase实时数据库中具有布尔值,我需要一个更新的值,而无需屏幕刷新。 我希望它可以在颤抖上进行更新

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2025-01-31 03:37:09

处理您的问题的一种简单方法是使用“ Cloud_firestore”软件包。
我将在下面制作UA示例代码。

注意:使用Firestore之前,您必须首先确保您已经初始化了Flutterfire!请点击此链接: https://firebase.flutter.flutter.docs.docs.docs/docs/overview/overveive/view/view/view/#初始化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(),
    );
  },
);
  }
}

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(),
    );
  },
);
  }
}
零崎曲识 2025-01-31 03:37:09

如果您在firebase实时数据库中有数据,则可以通过其 onvalue stream 。您可以使用streamBuilder

@override
Widget build(BuildContext context) {
  var ref = FirebaseDatabase.instance.ref('path_to_value');
  return Scaffold(
    StreamBuilder(
      stream: ref.onValue,
      builder: (context, snapshot) {
        if (snapshot.hasData) return Text(snapshot.data!.snapshot.value);
        if (!snapshot.hasData) return CircularProgressIndicator();
        if (snapshot.hasError) return Text(snapshot.error);
      }
    )
  )

我建议观看此出色的Code-Along视频: firebase实时数据库和扑朔迷离-Firecasts

If you have data in the Firebase Realtime Database, you can listen to update to that data through its onValue stream. You can use this stream in your widget tree by using a StreamBuilder:

@override
Widget build(BuildContext context) {
  var ref = FirebaseDatabase.instance.ref('path_to_value');
  return Scaffold(
    StreamBuilder(
      stream: ref.onValue,
      builder: (context, snapshot) {
        if (snapshot.hasData) return Text(snapshot.data!.snapshot.value);
        if (!snapshot.hasData) return CircularProgressIndicator();
        if (snapshot.hasError) return Text(snapshot.error);
      }
    )
  )

I recommend watching this great code-along video: The Firebase Realtime Database and Flutter - Firecasts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文