在 flutter 中获取 Firestore 集合时出现问题
Problem getting Firestore collection in flutter
我在 Flutter 中有一个新项目正在处理现有的 Firestore 数据库。
我似乎无法在列表视图中显示集合(甚至调试)。 Firestore 访问似乎还可以,因为我可以使用 Auth 模块。
如果我使用调试器并进入集合 get 函数,我可以看到正在返回数据,但没有触发 .then 函数。我是飞镖的新手,所以我很难弄清楚为什么数据没有冒泡到 .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())
);
}
}
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
使用 cloud_firestore:^3.1.17
这有帮助吗:
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());
}
}
我在 Flutter 中有一个新项目正在处理现有的 Firestore 数据库。 我似乎无法在列表视图中显示集合(甚至调试)。 Firestore 访问似乎还可以,因为我可以使用 Auth 模块。 如果我使用调试器并进入集合 get 函数,我可以看到正在返回数据,但没有触发 .then 函数。我是飞镖的新手,所以我很难弄清楚为什么数据没有冒泡到 .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())
);
}
}
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
使用 cloud_firestore:^3.1.17
这有帮助吗:
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());
}
}