使用共享首选项计算应用程序打开的次数

Count how many times the app is open using shared preferences

我有一个小部件,它应该在多次应用打开事件后显示在主屏幕上。我知道我必须使用共享首选项,但我不知道如何实现它。

整数值=0; // 当应用程序打开事件调用时获取 int 以检查上次值。

value = prefs.getInt("key") ?? 0 : prefs.getInt("密钥");

// 当应用程序打开事件调用时设置带有附加值的 int。

SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setInt("键",(值 + 1));

//当你想检查应用程序打开了多少次时你可以使用这个。

SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.getInt("钥匙");

现已解决:

  final SharedPreferences pref = await SharedPreferences.getInstance();
    var _count = pref.getInt(Constants.countAppPrefKey) ?? 0;
    _count++;
    pref.setInt(Constants.countAppPrefKey, _count);
    emit(state.copyWith(showFeedback: _count > WlConfig.countNumber));
  }