[英]Problem getting Firestore collection in flutter
我在 Flutter 中有一個新項目正在處理現有的 Firestore 數據庫。 我似乎無法在列表視圖中顯示集合(甚至調試)。 Firestore 訪問似乎還可以,因為我可以使用 Auth 模塊。 如果我使用調試器並進入集合 get function,我可以看到正在返回數據,但是沒有觸發 function。 我是 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 來自 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
使用 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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.