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());
},
),
),
這是控制臺中顯示的共享首選項中保存的串列的螢屏截圖:
這是帶有收藏夾按鈕的串列的螢屏截圖,我通過單擊收藏夾按鈕將文章保存在 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.
uj5u.com熱心網友回復:
在另一個頁面上,您必須獲得共享首選項實體。
final sharedPreferences = await SharedPreferences.getInstange();
然后你可以通過 sharedPreferences.getStringList('News'); 獲取你的串列。并通過 json.decoder 轉換為 json;
如果您想創建一個 Article 類的實體,那么您可以在其中實作一個方法,該方法將從 json 回傳該類的新實體。例如:
class Article {
factory Article fromJson(Map<String, dynamic> json) {
return Article(id: json['id'], ...);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/377563.html
