【问题标题】:How to Store locally Increment Counter in ListView? flutter如何在 ListView 中本地存储增量计数器?扑
【发布时间】: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();
                                        },
                                      ),
                                    ],
                                  );
}

),

【问题讨论】:

    标签: flutter sharedpreferences


    【解决方案1】:
    1. 首先添加shared_preferences

    2. 在你的类中创建一个变量SharedPreferences prefs;

    3. 初始化initState() 中的实例如:prefs = await SharedPreferences.getInstance()

    4. 当计数器被点击/增加/减少时,保存如下:
      await prefs.setInt('counter', counterValue);

    5. 下次打开应用程序时,检查initState() 是否有保存在首选项中的值。如果是,则使用,否则使用 0。
      例子: int counter = (prefs.getInt('counterValue') ?? 0);
      现在使用这个counter 变量来显示文本。

    【讨论】:

    • 所以当你收到来自 api 的投票时,将它们保存到 sharedPreferences。
    【解决方案2】:

    您可以使用shared_preferences

      Future<void> _storeIncrement(int yourValue) async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        await prefs.setInt('counter', yourValue);
      }
    

    还有更多选项可以根据您的数据类型进行存储,如下所示

    prefs.setBool(key, value)
    prefs.setString(key, value)
    prefs.setDouble(key, value)
    prefs.setStringList(key, value)
    

    下面的代码是获取数据

     Future<void> _getIncrement() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        int storedValue = prefs.getInt('counter');
        print('your data is $storedValue');
      }
    

    根据您的数据类型获取数据

    prefs.getBool(key)
    prefs.getString(key)
    prefs.getDouble(key)
    prefs.getStringList(key)
    

    【讨论】:

    • await SharedPreferences.getInstance(); 应该只被调用一次,以避免一次又一次地重新初始化实例。最好尽早初始化它。
    • @DarShan 是对的,SharedPreferences 实例应该被调用一次以避免重新初始化。但是,如果您感到困惑或新手,那么您可以按照我在上面的示例中提到的那样进行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多