【发布时间】:2021-11-26 19:39:59
【问题描述】:
我正在尝试在颤振中使用 shared_preferences 创建一个“收藏夹”函数。
使用此方法本地存储,无需数据库。
要使用的页面是globals.dart,带有bool变量,button.dart是页面选择窗口(添加书签时更改按钮颜色),
还有一个文件home0_01.dart 带有一个“收藏图标按钮(单击以将该按钮添加到收藏夹)”。
这里我想用shared_prefer实现它在本地存储
但即使查看大量示例代码,也很难实现。
我的代码中有数百个favoriteButton_0_01,因为home0_01.dart 文件太多了。在这种情况下我应该如何编码?
globals.dart
library prj.com.globals;
bool favoriteButton_0_01 = true;
bool favoriteButton_0_02 = false;
...
button.dart
...
LearnLevelButton(
color: favoriteButton_0_01 ? Colors.orange : Color(0xff7ba6f9),
text: '',
onTap: () async {
await Navigator.pushReplacement(context, MaterialPageRoute(builder: (context){
return home0_01();
}));
},
),
...
home0_01.dart
void delete() async {
setState(() {
favoriteButton_0_01 = false;
print(favoriteButton_0_01);
});
}
void saved() async {
setState(() {
favoriteButton_0_01 = true;
print(favoriteButton_0_01);
});
}
...
actions: <Widget>[
IconButton(
onPressed: favoriteButton_0_01 ? delete : saved,
icon: favoriteButton_0_01
? Icon(Icons.bookmark_rounded, color: Colors.yellow[800], size: 30)
: Icon(Icons.bookmark_add_outlined, color: Colors.yellow[800],size: 30),
),
],
...
【问题讨论】:
-
为什么你有数百个文件而不是做一些更有效率的事情?
-
这是因为每个文件都需要不同的 YouTube 视频和单独的“收藏夹”。
-
你这样做的方式似乎效率极低,难道你不能用自定义类之类的东西将视频存储在一个列表中,然后再生成它们吗?
-
我也是初学者,YouTube api和应用栏名称每次都在变化,但我找不到有效的方法来处理。
标签: flutter local-storage flutter-web flutter-sharedpreference