简体   繁体   English

在 flutter 中获取 Firestore 集合时出现问题

[英]Problem getting Firestore collection in flutter

I have a new project in Flutter working on an existing Firestore database.我在 Flutter 中有一个新项目正在处理现有的 Firestore 数据库。 I cannot seem to get the collection to surface in a list view (or even debugging).我似乎无法在列表视图中显示集合(甚至调试)。 Firestore access seems Ok as I can use the Auth module Ok. Firestore 访问似乎还可以,因为我可以使用 Auth 模块。 If I use the debugger and step into the collection get function, I can see the data is being returned, but the.then function is not being triggered.如果我使用调试器并进入集合 get function,我可以看到正在返回数据,但是没有触发 function。 Am new to dart so am having trouble trying to figure out why the data is not being bubbled up to the.then()我是 dart 的新手,所以我无法弄清楚为什么数据没有冒泡到 the.then()

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

class InviteView extends StatelessWidget {
  InviteView({Key? key}) : super(key: key);
  List<String> ids = [];
  @override
  Widget build(BuildContext context) {
    return Expanded(
        child: FutureBuilder(
          future: _getPlayers(),
          builder: (context, snapshot) {
            return ListView.builder(
              itemCount: ids.length,
              itemBuilder: (cxt, idx) {
                return ListTile(
                  title: Text(ids[idx]),
                );
              },
            );
          },
        ),
      );
  }

  Future<void> _getPlayers () async {
    const source = Source.server;

    await FirebaseFirestore.instance.collection('players').get(const GetOptions(source: source)).then(
            (snapshot) => snapshot.docs.map((document) {
              print(document.reference.id);
              ids.add(document.reference.id);
            }),
            onError: (e) => print (e.toString())
    );
  }
}

Output from flutter doctor Output 来自 flutter 医生

[√] Flutter (Channel stable, 3.0.1, on Microsoft Windows [Version 10.0.22000.675], locale en-AU)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2021.1)
[√] Android Studio (version 2021.2)
[√] Android Studio (version 4.1)
[√] VS Code (version 1.65.2)
[√] VS Code, 64-bit edition (version 1.32.3)
[√] Connected device (3 available)
[√] HTTP Host Availability

using cloud_firestore: ^3.1.17使用 cloud_firestore:^3.1.17

Does this help:这有帮助吗:

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

class InviteView extends StatelessWidget {
  InviteView({Key? key}) : super(key: key);
  List<String> ids = [];
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: FutureBuilder<Widget>(
        future: _getPlayers(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return const Text('Oh no!');
          return snapshot.data!;
        },
      ),
    );
  }

  Future<ListView> _getPlayers() async {
    var snap = await FirebaseFirestore.instance.collection('players').get();
    return ListView(
        children: snap.docs
            .map((doc) => ListTile(
                  title: Text(doc.reference.id),
                ))
            .toList());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM