如何在Flutter中显示Firebase集合的数量?

时间:2019-12-10 18:34:10

标签: firebase flutter google-cloud-firestore

我正在开发一个管理应用程序,因为我想显示云Firestore中的用户数(集合)。有人可以告诉我该怎么做。我屏幕上的内容看起来像这样[![在此处输入图片描述] [1]] [1] 这是我的整个代码,当我尝试运行它得到一个空值,但是在控制台中打印正确的值时,有人可以帮忙

class _AdminState extends State<Admin> {
  getUsersCount() {
    var length = 0;
    Firestore.instance.collection('users').getDocuments().then((myDocuments){
      print("${myDocuments.documents.length}");
      length = myDocuments.documents.length;
    });
    return length.toString();


 Page _selectedPage = Page.dashboard;
      MaterialColor active = Colors.indigo;
      MaterialColor notActive = Colors.grey;
      final databaseReference = Firestore.instance;



      bool isDelete= true;

      TextEditingController categoryController = TextEditingController();
      TextEditingController brandController = TextEditingController();
      GlobalKey<FormState> _categoryFormKey = GlobalKey();
    //GlobalKey<FormState> _brandFormKey = GlobalKey();
    // BrandService _brandService = BrandService();
       CategoryService _categoryService = CategoryService();




      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Row(
                children: <Widget>[
                  Expanded(
                      child: FlatButton.icon(
                          onPressed: () {
                            setState(() => _selectedPage = Page.dashboard);
                          },
                          icon: Icon(
                            Icons.dashboard,
                            color: _selectedPage == Page.dashboard
                                ? active
                                : notActive,
                          ),
                          label: Text('Dashboard'))),
                  Expanded(
                      child: FlatButton.icon(
                          onPressed: () {
                            setState(() => _selectedPage = Page.manage);
                          },
                          icon: Icon(
                            Icons.sort,
                            color:
                            _selectedPage == Page.manage ? active : notActive,
                          ),
                          label: Text('Manage'))),
                ],
              ),
              elevation: 0.0,
              backgroundColor: Colors.white,
            ),
            body: _loadScreen());
      }

      Widget _loadScreen() {
        switch (_selectedPage) {
          case Page.dashboard:
            return Column(
              children: <Widget>[
                ListTile(
                  subtitle: Text(
                    'Admin View',
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: 29.0, color: Colors.indigo,fontWeight: FontWeight.bold),
                  ),
                ),
                Expanded(
                  child: GridView(
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,


                    ),
                    children: <Widget>[
                      Padding(

                        padding: const EdgeInsets.all(18.0),
                        child: Card(

                          child: ListTile(
                              title: FlatButton.icon(

                                  onPressed: null,
                                  icon: Icon(Icons.directions_boat,color: Colors.black,),
                                  label: Text("Boats",style: TextStyle(fontSize: 9,color: Colors.indigo),)),
                              subtitle: Text(
                                '3',
                                textAlign: TextAlign.center,
                                style: TextStyle(color: active, fontSize: 50.0),
                              )),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(18.0),
                        child: Card(

                          child: ListTile(
                              title: FlatButton.icon(
                                  onPressed: null,
                                  icon: Icon(Icons.directions_boat,color: Colors.black,),
                                  label: Text("Boats",style: TextStyle(fontSize: 9,color: Colors.indigo),)),
                              subtitle: Text(
                                getUsersCount(),
                                textAlign: TextAlign.center,
                                style: TextStyle(color: active, fontSize: 50.0),
                              )),
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(22.0),
                        child: Card(
                          child: ListTile(
                              title: FlatButton.icon(
                                  onPressed: null,
                                  icon: Icon(Icons.bookmark,color: Colors.black,),
                                  label: Text("Bookings",style: TextStyle(fontSize: 8,color: Colors.indigo),)),
                              subtitle: Text(
                                '120',
                                textAlign: TextAlign.center,
                                style: TextStyle(color: active, fontSize: 50.0),
                              )),
                        ),
                      ),
                    ],

                  ),

                ),
              ],
            );
            break;

1 个答案:

答案 0 :(得分:1)

@Rohit,您尝试过这种方法吗?

Firestore.instance.collection('users').getDocuments().then((myDocuments){
 print("${myDocuments.documents.length}");
});

假设您要在字幕中显示它,则可以尝试以这种方式使用它(只是为了给您一个主意),

  • 创建一个获取计数的函数,
getUsersCount() {
  var length = 0;
  Firestore.instance.collection('users').getDocuments().then((myDocuments){
    print("${myDocuments.documents.length}");
    length = myDocuments.documents.length;
  });
  return length.toString();
}
  • 称呼它,
return FutureBuilder(
            future: getUsersCount(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                Column(
                  children: <Widget>[
                    ListTile(
                      subtitle: Text(
                        'Admin View',
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: 29.0,
                            color: Colors.indigo,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                    Expanded(
                      child: GridView(
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,


                        ),
                        children: <Widget>[
                          Padding(

                            padding: const EdgeInsets.all(18.0),
                            child: Card(

                              child: ListTile(
                                  title: FlatButton.icon(

                                      onPressed: null,
                                      icon: Icon(Icons.directions_boat,
                                        color: Colors.black,),
                                      label: Text("Boats", style: TextStyle(
                                          fontSize: 9, color: Colors.indigo),)),
                                  subtitle: Text(
                                    '3',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        color: active, fontSize: 50.0),
                                  )),
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(18.0),
                            child: Card(

                              child: ListTile(
                                  title: FlatButton.icon(
                                      onPressed: null,
                                      icon: Icon(Icons.directions_boat,
                                        color: Colors.black,),
                                      label: Text("Boats", style: TextStyle(
                                          fontSize: 9, color: Colors.indigo),)),
                                  subtitle: Text(
                                    snapshot.data,
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        color: active, fontSize: 50.0),
                                  )),
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(22.0),
                            child: Card(
                              child: ListTile(
                                  title: FlatButton.icon(
                                      onPressed: null,
                                      icon: Icon(
                                        Icons.bookmark, color: Colors.black,),
                                      label: Text("Bookings", style: TextStyle(
                                          fontSize: 8, color: Colors.indigo),)),
                                  subtitle: Text(
                                    '120',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        color: active, fontSize: 50.0),
                                  )),
                            ),
                          ),
                        ],

                      ),

                    ),
                  ],
                );
              } else {
                Center(child: CircularProgressIndicator())
              }
            });