使用Timer从Firebase数据库检索数据

时间:2020-04-09 16:27:24

标签: database firebase flutter timer

如何在Flutter应用中使用Timer函数从Firebase数据库中检索数据?

P.S。5分钟后将在Firebase数据库中检索用户请求数据。

谢谢

7 个答案:

答案 0 :(得分:0)

  Future.delayed(Duration(minutes: 5), () {
      Timer.periodic(Duration(minutes: 5), (Timer t) {
        setState(() {});
      });
    });

好的,与您一起StreamBuilder放在有状态的小部件中,这在我的模拟器上有效。让我知道它是否对您有用。

答案 1 :(得分:0)

我的代码如下所示,但是无法与您的代码一起使用。@ wcyankees424

body: StreamBuilder(
        stream: qs.collection('collection').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            const Text('Loading');
            return Center(
              child: CircularProgressIndicator(),
            );
          } else {
            return ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (context, index) {
                  List<DocumentSnapshot> listedQS = snapshot.data.documents;
                  var random = new Random();
                  for (var i = listedQS.length - 1; i > 0; i--) {
                    var n = random.nextInt(i + 1);
                    var temp = listedQS[i];
                    listedQS[i] = listedQS[n];
                    listedQS[n] = temp;
                  }

                  DocumentSnapshot mypost = listedQS[0];

答案 2 :(得分:0)

 Future<void> addEntry(Brew newBrew) async {
    //value you want saved are stored in newBrew and passed in
    Map<String, Object> entryData = {
      'name': newBrew.name,
      'strength': newBrew.strength,
      'sugars': newBrew.sugars,
      'index': newBrew.index,
    };
    await Firestore.instance.collection('//collection name').add(entryData);
  }

 Future<Brew> getEntries(Brew newBrew) async {
    QuerySnapshot snapshot = await Firestore.instance
        .collection('//Collection name')
        .where('index', isGreaterThanOrEqualTo: Random().nextInt('//this number should be higher than the number of documents'))
        .orderBy('index')
        .limit(1)
        .getDocuments();

    if (snapshot.documents.isNotEmpty) {
      Map<String, dynamic> documentData = snapshot.documents[0].data;

      return Brew(
        strength: documentData['strngth'],
        sugars: documentData['sugars'],
        name: documentData['name'],
        index: documentData['index'],
      );
    } else {
      snapshot = await Firestore.instance
          .collection('//Collection name')
          .where('index', isGreaterThanOrEqualTo: 0)
          .orderBy('index')
          .limit(1)
          .getDocuments();

      Map<String, dynamic> documentData = snapshot.documents[0].data;

      return Brew(
        strength: documentData['strngth'],
        sugars: documentData['sugars'],
        name: documentData['name'],
        index: documentData['index'],
      );
    }
  }

class Brew {
  final String name;
  final String sugars;
  final int strength;
  final int index;

  Brew({
    this.name,
    this.sugars,
    this.strength,
    this.index,
  });
}

您将为每个被调用的索引创建和字段,该索引将从数据库中每个条目的0开始以1递增。 This might help you

答案 3 :(得分:0)

我的代码如下所示,如何使用Querysnapshot方法显示文本和图像值?您已经用brew代码提到了!我无法将您的代码用于以下代码结构...

您的代码:@ wcyankees424

   snapshot = await Firestore.instance
      .collection('//Collection name')
      .where('index', isGreaterThanOrEqualTo: 0)
      .orderBy('index')
      .limit(1)
      .getDocuments();

我的代码:

body: StreamBuilder(
        stream: qs.collection('collection').limit(1).snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            const Text('Loading');
          } else {
            return ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (context, index) {
                  List<DocumentSnapshot> listedQS =
                      snapshot.data.documents; //listed documents
                  var random = new Random(); //dart math
                  for (var i = listedQS.length - 1; i > 0; i--) {
                    var n = random.nextInt(i + 1);
                    var temp = listedQS[i];
                    listedQS[i] = listedQS[n];
                    listedQS[n] = temp;
                  }
                  DocumentSnapshot mypost = listedQS[0];
                  return Stack(
                    children: <Widget>[
                      Container(
                        width: MediaQuery.of(context).size.width,
                        height: 350,
                        child: Padding(
                          padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
                          child: Material(
                            color: Colors.white,
                            elevation: 14.0,
                            shadowColor: Color(0x882196F3),
                            child: Center(
                              child: Padding(
                                padding: EdgeInsets.all(8.0),
                                child: Column(
                                  children: <Widget>[
                                    Container(
                                      width:
                                          MediaQuery.of(context).size.width,
                                      height: 200,
                                      child: Image.network(
                                          '${mypost['image']}',
                                          fit: BoxFit.fill),
                                    ),
                                    SizedBox(height: 10.0),
                                    Text(
                                      '${mypost['title']}',
                                      style: TextStyle(
                                          fontSize: 20.0,
                                          fontWeight: FontWeight.bold),
                                    ),
                                    SizedBox(height: 10.0),
                                    Text(
                                      '${mypost['subtitle']}',
                                      style: TextStyle(
                                          fontSize: 16.0,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.blueGrey),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),

答案 4 :(得分:0)

input

答案 5 :(得分:0)

Hope this helps因此,您通过调用where('index' isEqualTo: ' 0'来执行的操作是:首先找到具有上述索引字段的任何文档,然后仅返回具有等于该值的文档您提供了

答案 6 :(得分:0)

我的代码如下。我也按照上面的指示更新了数据库,但屏幕仍然白屏

Widget build(BuildContext context) {
return Scaffold(
    body: StreamBuilder(
        stream: Firestore.instance
            .collection('fortunepool')
            .where('index', isGreaterThanOrEqualTo: 0)
            .orderBy('index')
            .limit(1)
            .snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Container(child: CircularProgressIndicator());
          } else {
            return ListView.builder(
                itemCount: snapshot.data.documents.length,
                itemBuilder: (context, index) {
                  List<DocumentSnapshot> listedQS =
                      snapshot.data.documents; //listed documents

                  DocumentSnapshot mypost = listedQS[0];
                  return Stack(