flutterdartratingbardart-packages

Rating becomes zero when i scroll down in flutter


I am using flutter rating bar package to give feedback to particular section then rating becomes back to 0. ho w can i persist the given rating constant .

Here is the screenshot of the app ...

  RatingBar(
                                    initialRating: 0,
                                    direction: Axis.horizontal,
                                    allowHalfRating: false,
                                    itemCount: 5,
                                    ratingWidget: RatingWidget(
                                        full: const Icon(Icons.star,
                                            color: Colors.orange),
                                        half: const Icon(
                                          Icons.star_half,
                                          color: Colors.orange,
                                        ),
                                        empty: const Icon(
                                          Icons.star_outline,
                                          color: Colors.orange,
                                        )),
                                    onRatingUpdate: (value) {}),

enter image description here


Solution

  • i think its a flutter behavior. in case we have much children on listview, then when we scrolldown, the widget that out of screen will marked as dirty widget. then when we back scroll again , flutter will rebuild the widget.

    Flutter already provide the solution here called

    Destruction mitigation

    you have to store the rating value to the object state. so when the widget get re-build , the value will automatically set from stored value.

    other simple solution (not recomended) you can extend the cache of listview.

    ListView(
     shrinkwrap: true,
     cacheExtent : 99999999 
     children: [
       RatingWidget(),
      ],
    

    here the explanation to the chaceExtent

    or another question in stackoverflow What exactly does cacheExtent property in ListView.Builder do?