flutter - 打开应用程序时,除了第一次打开外,如何一直做一个动作
问题描述
我搜索如何为我的 statefull 小部件的 initstate 的每次调用执行第一次调用之外的操作。
谢谢
解决方案
最好的方法是使用将值保存到本地存储的SharedPreferences插件。
- 将依赖项添加到您的 pubspec.yaml 文件
dependencies: shared_preferences: ^0.5.6
- 导入插件
import 'package:shared_preferences/shared_preferences.dart';
- 引用 SharedPreferences 类
SharedPreferences prefs = await SharedPreferences.getInstance();
最后检查是否是第一次
// If the preference firstTime does not exist it means it is the first time bool firstTime = prefs.getBool('firstTime') ?? true; if(firstTime){ // This is the first time the user is opening the app // Set firstTime preference value to false await prefs.setBool('firstTime', false); } else{ // The app has been opened before }
推荐阅读
- java - 当我有 ManyToMany 关系(spring-boot/spring-data-jpa)时如何更新子实体列表?
- python-3.x - How do I get my function to return something other than a reference?
- regex - 创建一个正则表达式范围,其中两个十进制数字作为可选数字
- tableau-api - 无需手动操作即可加载嵌入式画面 - Tableau
- symfony - 自定义 FormType 中的自定义变量
- go - Windows api dll 库加载指南
- javascript - 第一次尝试使用redux,收到错误消息“无法读取未定义的属性调度”
- python-3.x - 如何在 Beautifulsoup 的多个列表中获取特定元素?
- c++ - 如何在(C++)构造函数参数列表中包含 std:list?
- javascript - 如何解决 Typescript 中有关 `withStateHandlers` 的错误