0

我在我的应用程序上设置了 Flutter 评分栏,让我的用户可以对另一个用户进行评分。.来自依赖项 Flutter Rating Bar的代码

如下

RatingBar.builder(
              initialRating: 1,
              minRating: 1,
              direction: Axis.horizontal,
              allowHalfRating: false,
              itemCount:5,
              itemPadding: EdgeInsets.symmetric(horizontal: 4.0),
              itemBuilder: (context, _) => Icon(
                Icons.star,
                color: Colors.orange,
              ),
              onRatingUpdate: (rating) {
                 print(rating);
              },
            ),

我想获取点击的星星的值并将其用于 onPressed 函数以将值推送到数据库。然后我需要使用以下函数将值移动到firestore:

CollectionReference addReview = FirebaseFirestore.instance.collection('Consultant');
  Future<void> createReview() {
    return addReview
        .doc(widget.snapshot['id']).collection('Reviews')
        .add(
      { 'reviewsValue': rating,
      },
    ).then((value) => print("Reviews Added"))
        .catchError((error) => print("Failed to add user: $error"));
  }
4

1 回答 1

0
This is work for me
step 1: declare variable (double)
        ex: double _currentIndex=0;
[enter image description here][1]
step 2: in the onRatingUpdate:(rating){
    _currentIndex=rating;
    print(rating);
}
[enter image description here][2]
step 3: create buttom with onPressed method
     ex 
      TextButton(){`enter code here`
        onPressed(){
         print("current index ${_currentIndex}");
         //now you can put this value into your method saveData
       }}
[enter image description here][3]
this is the result
[enter image description here][4]


  [1]: https://i.stack.imgur.com/pYyHH.png
  [2]: https://i.stack.imgur.com/Yb4It.png
  [3]: https://i.stack.imgur.com/RLsWy.png
  [4]: https://i.stack.imgur.com/NId3t.png
于 2022-03-04T05:57:03.100 回答