【发布时间】:2021-05-25 12:19:00
【问题描述】:
我正在谈论来自网站的 API 请求,并且有一个投票我想在本地存储在颤振中。我已经实现了投票的递增和递减,但我想将投票本地存储在手机中。
如何在listView中本地存储递增和递减计数器?
class MoviesModel {
int vote;
MoviesModel({this.vote});
int increaseCounter() {
vote++;
return vote;
}
void decreaseCounter() {
if (vote > 0) {
vote--;
}
}
}
下面是 listView 构建器
ListView.builder(
itemCount: _movies.length,
padding: EdgeInsets.all(4),
physics: BouncingScrollPhysics(),
itemBuilder: (context, index) {
final moviess = _movies[index];
return Column(
children: [
IconButton(
icon: Icon(
Icons.keyboard_arrow_up_outlined,
),
color: Colors.white,
onPressed: () async {
final prefs = await SharedPreferences
.getInstance();
setState(() {
final vote =
moviess.increaseCounter();
prefs.setInt('vote', vote);
print(vote);
});
},
),
SizedBox(
height: 10,
),
// moviess.vote.toString(),
Text(moviess.vote.toString() ?? " ",
style: TextStyle(
color: Colors.white,
fontSize: 20)),
SizedBox(
height: 10,
),
IconButton(
icon: Icon(
Icons.keyboard_arrow_down_outlined,
),
color: Colors.white,
onPressed: () {
setState(() {
moviess.decreaseCounter();
});
// decreaseCount();
},
),
],
);
}
),
【问题讨论】: