读取Flutter Firestore文档和排序

huangapple go评论64阅读模式
英文:

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的数据。无论我怎么努力查找,都找不到。请帮我。

这是Firestore的截图

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 &#39;package:flutter/material.dart&#39;;
import &#39;package:firebase_auth/firebase_auth.dart&#39;;
import &#39;package:cloud_firestore/cloud_firestore.dart&#39;;
final firestore = FirebaseFirestore.instance;
class championship extends StatefulWidget {
const championship({Key? key}) : super(key: key);
@override
State&lt;championship&gt; createState() =&gt; _championshipState();
}
class _championshipState extends State&lt;championship&gt; {
///Documents Ids
List&lt;String&gt; docIDs = [];
int a = 0;
///Get docIDs
Future getDocId() async {
if(a==0){
a++;
await FirebaseFirestore.instance.collection(&#39;championship&#39;).get().then(
(snapshot) =&gt; 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(&#39;championship&#39;,
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)
),
),
);
},
);
},
),
)
],
),
),
);
}
}

above code execution screen

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

Here's the fire store

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().

huangapple
  • 本文由 发表于 2023年6月22日 08:06:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定