英文:
Read the flutter firestore document and collation
问题
我们已成功将Flutter Firestore文档数据读取到Listview.Builder中。以下是代码部分:
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
final firestore = FirebaseFirestore.instance;
class Championship extends StatefulWidget {
const Championship({Key? key}) : super(key: key);
@override
State<Championship> createState() => _ChampionshipState();
}
class _ChampionshipState extends State<Championship> {
///文档ID
List<String> docIDs = [];
int a = 0;
///获取文档ID
Future<void> getDocId() async {
if (a == 0) {
a++;
await FirebaseFirestore.instance.collection('championship').get().then(
(snapshot) => snapshot.docs.forEach(
(document) {
// print(document.reference);
docIDs.add(document.reference.id);
},
),
);
}
}
@override
void initState() {
if (a == 1) {
getDocId();
}
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
'championship',
style: TextStyle(color: Colors.black, fontSize: 17),
),
backgroundColor: Colors.white70,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(
height: 30,
),
const SizedBox(
height: 10,
),
Expanded(
child: FutureBuilder(
future: getDocId(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: docIDs.length,
itemBuilder: (context, index) {
return ListTile(
title: ElevatedButton(
child: Text(
docIDs[index],
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return CompetitionMainPage();
},
),
);
},
style: ButtonStyle(
backgroundColor:
MaterialStateProperty.all(Colors.black38),
),
),
);
},
);
},
),
),
],
),
),
);
}
}
当我按下20231001时,我希望显示20231001的数据,当我按下20231008时,我希望显示20231008的数据。无论我怎么努力查找,都找不到。请帮我。
在competition_main_page
中使用以下代码来接收place
参数:
class CompetitionMainPage extends StatelessWidget {
final String place;
CompetitionMainPage({required this.place});
// 其他部分的代码
}
然后,您可以在CompetitionMainPage
中使用place
变量,如下所示:
Text(place); // 在需要显示place的地方使用它
这样,您就可以在CompetitionMainPage
中访问和显示传递的place
参数。
英文:
We have successfully read the flutter Firestore Documentation data into the Listview.Builder
The code is as follows
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
final firestore = FirebaseFirestore.instance;
class championship extends StatefulWidget {
const championship({Key? key}) : super(key: key);
@override
State<championship> createState() => _championshipState();
}
class _championshipState extends State<championship> {
///Documents Ids
List<String> docIDs = [];
int a = 0;
///Get docIDs
Future getDocId() async {
if(a==0){
a++;
await FirebaseFirestore.instance.collection('championship').get().then(
(snapshot) => snapshot.docs.forEach(
(document) {
// print(document.reference);
docIDs.add(document.reference.id);
}
),
);
}
}
@override
void initState() {
if(a == 1){
getDocId();
}
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('championship',
style: TextStyle(color: Colors.black, fontSize: 17),
),
backgroundColor: Colors.white70,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(height: 30,),
const SizedBox(height: 10,),
Expanded(
child: FutureBuilder(
future: getDocId(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: docIDs.length,
itemBuilder: (context, index){
return ListTile(
title: ElevatedButton(
child: Text(docIDs[index],
style: TextStyle(
color: Colors.white, fontSize: 20),
),
onPressed: (){
Navigator.push(context, MaterialPageRoute(
builder: (context){
return competition_main_page(
);
}
),
);
},
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.black38)
),
),
);
},
);
},
),
)
],
),
),
);
}
}
What I want is to show 20231001 data when I press 20231001 and 20231008 data when I press 20231008
No matter how hard I look, I can't find it Please help me
return competition_main_page(place:snapshot.data[index].data['place']);
Then, receive String place; from composition_main_page and required this.I tried to place, etc., but failed
答案1
得分: 0
Change From:
snapshot.data[index].data['place']
Change To:
snapshot.data[index].data()['place']
You also need to pass the DocumentSnapshot to competition_main_page().
英文:
Change From:
snapshot.data[index].data['place']
Change To:
snapshot.data[index].data()['place']
You also need to pass the DocumentSnapshot to competition_main_page().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论