如何解决如何使用提供程序和sqflite数据库颤动将喜欢的项目保存在语言环境存储设备中
大家好,我在这里遇到问题,我使用提供程序包在“收藏夹”页面中添加了收藏夹项目,工作正常,因此当我重新启动应用程序“收藏夹项目”时,我想将其保留在语言环境存储中,有人可以帮助我解决使用sqlite数据库会出现此问题。
感谢您解决这个问题
这是使用提供商最喜欢的模型
class FavoriteItem {
String id;
String image;
String title;
String typeFood;
String timeFood;
double price;
FavoriteItem(this.id,this.image,this.title,this.typeFood,this.timeFood,this.price);
}
class Favorite with ChangeNotifier{
Map<String,FavoriteItem> _items = {};
Map<String,FavoriteItem> get items {
return {..._items};
}
int get itemCount {
return _items.length;
}
void addFavoriteItem(String foodId,String foodImage,String foodTitle,String foodType,String foodTime,double foodPrice){
if(_items.containsKey(foodId)){
_items.update(foodId,(existingFavoriteItem) =>
FavoriteItem(
DateTime.Now().toString(),existingFavoriteItem.image,existingFavoriteItem.title,existingFavoriteItem.typeFood,existingFavoriteItem.timeFood,existingFavoriteItem.price
)
);
}else {
_items.putIfAbsent(
foodId,() =>
FavoriteItem(
DateTime.Now().toString(),foodImage,foodTitle,foodType,foodTime,foodPrice,));
}
notifyListeners();
}
void removeItem(String id) {
_items.remove(id);
notifyListeners();
}
void clear() {
_items = {};
notifyListeners();
}
}
这是收藏页面
class FavoritePage extends StatefulWidget {
@override
_FavoritePageState createState() => _FavoritePageState();
}
class _FavoritePageState extends State<FavoritePage> {
@override
Widget build(BuildContext context) {
final favorite = Provider.of<Favorite>(context);
return Scaffold(
backgroundColor: Color(0xffFFFAEE),body: SafeArea(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(15),child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,children: <Widget>[
Text(
"Your Favorite Foods",style: TextStyle(
fontSize: 20,fontWeight: FontWeight.bold,color: Color(0xff544646)
),),Row(
children: <Widget>[
Tooltip(
message: "Delete All",textStyle: TextStyle(
fontWeight: FontWeight.bold,color: Color(0xffFfdb84)
),decoration: Boxdecoration(
color: Colors.black,borderRadius: BorderRadius.circular(6)
),child: GestureDetector(
onTap: (){
showDialog(
context: context,builder: (BuildContext context) => AlertDialog(
title: Text(
"Do you want delete this food?",style: TextStyle(
fontSize: 18,color: Color(0xff544646),fontWeight: FontWeight.w600
),shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(15))
),backgroundColor: Color(0xffFFFAEE),actions: <Widget>[
FlatButton(
onpressed: (){
Navigator.of(context).pop(false);
},child: Text(
"No",style: TextStyle(
color: Colors.black,fontWeight: FontWeight.bold
),)
),FlatButton(
onpressed: (){
Navigator.of(context).pop(true);
Provider.of<Favorite>(context,listen: false).clear();
},child: Text(
"Yes",)
)
],)
);
},child: Container(
height: 40,width: 40,decoration: Boxdecoration(
color: Color(0xffFfdb84),borderRadius: BorderRadius.circular(100)
),child: Icon(
Icons_BottomBar.delete,size: 22,],SizedBox(
height: 10,Expanded(
// If favorite items is empty show image and text " favorite is empty "
child: favorite.items.isEmpty
? Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
Container(
height: 260,width: 260,child: SvgPicture.asset(
favorite_empty
)),Text(
"Favorite is Empty",style: TextStyle(
fontSize: 20,)
],)
// else
:ListView.builder(
itemCount: favorite.items.length,itemBuilder: (context,index) =>
FavoriteCardItem(
favorite.items.values.toList()[index].id,favorite.items.keys.toList()[index],favorite.items.values.toList()[index].image,favorite.items.values.toList()[index].title,favorite.items.values.toList()[index].typeFood,favorite.items.values.toList()[index].timeFood,favorite.items.values.toList()[index].price,)
),SizedBox(
height: 70,)
);
}
}
Padding(
padding: const EdgeInsets.only(right: 10),child: GestureDetector(
onTap: () async {
setState(() {
ispressed = true;
});
Scaffold.of(context).showSnackBar(SnackBar(
backgroundColor: Colors.black,content: Text(
"Item Added to Favorite",style: TextStyle(
color: Color(0xffFfdb84)
),duration: Duration(seconds: 3),));
favorite.addFavoriteItem(
loadFood.id,loadFood.image,loadFood.title,loadFood.typeFood,loadFood.timeFood,loadFood.price
);
// await helper.createFavorite(favorite.favoriteItem);
},child: Container(
height: 35,width: 35,decoration: Boxdecoration(
borderRadius: BorderRadius.circular(20),color: Color(0xffFfdb84)
),// active favorite icon when clicked
child: (ispressed)
?Icon(Icons.favorite,size: 20,color: Color(0xffff124d))
:Icon(Icons.favorite_border,color: Color(0xffff124d)),
here is screenshot about favorite page
解决方法
您应该保存ID列表。方法如下:
// Shared preferences : favorites list key
const favoritesKey = "FAVORITES_KEY";
// Turn the list of favorites into a list of their id
var ids = <String>[ for(var item in items) item.id ];
SharedPreferences prefs = await SharedPreferences.getInstance();
// Save the list
prefs.setStringList(favoritesKey,ids);
// Retrieve the list
var ids = prefs.getStringList(favoritesKey);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。