如何使用提供程序和sqflite数据库Flutter在Locale存储设备中保存收藏夹项目



大家好,我这里有问题,我使用provider包在收藏夹页面中添加收藏夹项目,工作正常,所以当我重新启动应用程序时,收藏夹项目消失了,我想把项目保存在区域设置存储中,任何人都可以帮助我如何使用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",
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold
),
)
)
],
)
);
},
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, size: 20,color: Color(0xffff124d)),
),
),
),

这是关于最喜欢的页面的屏幕截图

您应该保存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);

最新更新