Home > OS >  Problem getting Firestore collection in flutter
Problem getting Firestore collection in flutter

Time:06-03

I have a new project in Flutter working on an existing Firestore database. 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. 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. Am new to dart so am having trouble trying to figure out why the data is not being bubbled up to 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);
            }),
            one rror: (e) => print (e.toString())
    );
  }
}

Output from flutter doctor

[√] 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

CodePudding user response:

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());
  }
}
  • Related