我想从Firestore中选择随机数据

时间:2018-09-10 06:27:04

标签: flutter google-cloud-firestore

enter image description here

我正在使用测验应用程序,其中选择对手屏幕是屏幕截图。 当用户按下随机对手按钮时,我想在那里随机选择对手。以下是我的代码...

new StreamBuilder<QuerySnapshot>(
              stream: Firestore.instance.collection(USERS_COLLECTION).snapshots(),
              builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot,) {
                if (!snapshot.hasData)
                  return new Container(child: Text(""),);
                var documentsLength = snapshot.data.documents.length;
                int rand = new Math.Random().nextInt(documentsLength.toInt());
                return Column(
                  children: <Widget>[
                    Expanded(child: new ListView.builder(itemCount: documentsLength,
                        scrollDirection: Axis.vertical,
                        itemBuilder: (context, index) {
                          return buildListItem((Alluser.fromDocument(
                              snapshot.data.documents[index])),context);
                        }
                    )),
                    new RaisedButton(onPressed: (){
                              Navigator.of(context).push(new MaterialPageRoute(builder: (context)=> new ChallengePage(currentusername: _username,currentuseremail: _useremail,
        currentuserImage: _userimage,changeuseremail: snapshot.data.documents[rand].data["email"],chalangeuserImage: snapshot.data.documents[rand].data["userImage"],chalangeusername: snapshot.data.documents[rand].data["displayName"],)));
                    },child: Text("RANDOM OPPONENT"),)
                  ],
                );
              }
          ),

0 个答案:

没有答案