Hello community, I'm new to the Flutter world and mobile app development and struggling with How to display a saved list in Shared preferences in another list in another page(Favorite page
The favorite button to save the list in
共享首选项:
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: FlatButton(
child: Icon(
Icons.favorite,
color: Colors.green,
size: 25,
),
onPressed: () async {
SharedPreferences prefs =
await SharedPreferences.getInstance();
Article savedArticle = Article(
source: widget.list[i].source,
author: widget.list[i].author,
title: widget.list[i].title,
description: widget.list[i].description,
url: widget.list[i].url,
urlToImage: widget.list[i].urlToImage,
publishedAt: widget.list[i].publishedAt,
content: widget.list[i].content);
String json = jsonEncode(savedArticle);
// print('saved... $json');
list.add(json);
prefs.setStringList('News', list);
print("shared..." +
prefs.getStringList('News').length.toString());
},
),
),
这是控制台中显示的共享pref中保存的列表的屏幕截图:
这是一张带有收藏夹按钮的列表截图,我通过点击收藏夹按钮将文章保存在SharedPref中:
My goal is to display the saved list in the shared preferences in another page (favorite articles page) in a list but without favorite button. Could any one help me PLEASE ? Thank you.
在另一个页面上,您必须获得共享的首选项实例。
final sharedPreferences = await SharedPreferences.getInstange();
然后您可以通过sharedPreferences.getStringList("新闻"(获取您的列表;并通过json.delector转换为json;
如果您想创建Article类的实例,那么您可以在其中实现一个方法,该方法将从json返回该类的新实例。例如:
class Article {
factory Article fromJson(Map<String, dynamic> json) {
return Article(id: json['id'], ...);
}
}