扑来 - 使用共享首选项将多个变量保存到列表中

发布于 2025-02-04 19:49:56 字数 2329 浏览 3 评论 0原文

对于我的应用程序,我一直在尝试制作一个书签功能,该功能将保存列表并在书签页面上显示。我目前有两个问题,首先,每当我尝试保存数据时,例如ListingId,我都可以抓住它并将其存储得很好。但是,当我尝试保存另一个列表时,它用新的列表替换了旧列表。我已经尝试将保存的变量变成一个字符串并做类似

等待_preference的操作?

但是 +变量不会更改任何内容,不确定这是否是因为我如何构造我的代码,或者不是共享播放方式的工作方式。

然后,要在书签页面上显示整个列表,我希望能够获取所有重要数据,例如ListingID,ListingName,ListingDescription等在一张表中,希望能够根据其索引或列表ID删除/Unbook标记列表。

感谢任何建议或帮助!

这是我应用程序的一些代码。

user_simple_preferences.dart

import 'package:shared_preferences/shared_preferences.dart';

class UserSimplePreferences {

  static SharedPreferences? _preferences;
  static String _keyBookmarks = '';


  static Future init() async =>
      _preferences = await SharedPreferences.getInstance();

  static Future setBookmarks(String grabbedID) async =>
  await _preferences?.setString(_keyBookmarks, grabbedID);

  static String? getBookmarks() => _preferences?.getString(_keyBookmarks);
} 

listingdetail.dart.dart

//Triggers on button press

   bool isBookmarked = false;

   void toggleBookmark() {
     setState(() async {
       if (isBookmarked) {
         isBookmarked = false;
         print('IsFalse');
       } else {
         isBookmarked = true;
         print('isTrue');
         String grabbedID = widget.ListingID.toString();
         await UserSimplePreferences.setBookmarks(grabbedID);
         Navigator.push(
             context,
             MaterialPageRoute(
               builder: (context) =>
                   bookmarkPage(bookmarkedID: widget.ListingID),
             ));
       }
     });
   }

//My button for bookmarks
FavoriteButton(
                           iconDisabledColor: Colors.grey,
                           iconSize: 50,
                           isFavorite: isBookmarked,
                           valueChanged: (_) {
                             toggleBookmark();
                           },
                         ),

bookmarks.dart

class bookmarkPage extends StatefulWidget {
 final int bookmarkedID;

 const bookmarkPage({
   Key? key,
   required this.bookmarkedID,
 }) : super(key: key);

//rest of code...
//This grabs the saved bookmark and works on app restart.
Text(UserSimplePreferences.getBookmarks() ?? ''),


for my app I've been trying to make a bookmarking feature that will save listings and display them on the bookmark page. I'm currently having two issues with this, firstly that whenever I try to save data, for example, ListingID, I'm able to grab it and store it perfectly fine. But when I try to save another listing, it replaces the old ListingID with the new one. I've tried turning the saved variable into a string and doing something like

await _preferences?.setString(_keybookmarks, savedID + grabbedID);

But the + variable doesn't change anything, not sure if it's because of how I've structured my code or it's not how SharedPreferences work.

And then for displaying the whole listing on the bookmark page, I want to be able to grab all the important data such as ListingID, ListingName, ListingDescription, etc, and save it, perhaps in a map and be able to have multiple of these listings in one table, and hopefully be able to delete/unbookmark the listings based on their index or ListingID.

Thanks for any advice or help!

Here's some of my app's code.

user_simple_preferences.dart

import 'package:shared_preferences/shared_preferences.dart';

class UserSimplePreferences {

  static SharedPreferences? _preferences;
  static String _keyBookmarks = '';


  static Future init() async =>
      _preferences = await SharedPreferences.getInstance();

  static Future setBookmarks(String grabbedID) async =>
  await _preferences?.setString(_keyBookmarks, grabbedID);

  static String? getBookmarks() => _preferences?.getString(_keyBookmarks);
} 

listingDetail.dart

//Triggers on button press

   bool isBookmarked = false;

   void toggleBookmark() {
     setState(() async {
       if (isBookmarked) {
         isBookmarked = false;
         print('IsFalse');
       } else {
         isBookmarked = true;
         print('isTrue');
         String grabbedID = widget.ListingID.toString();
         await UserSimplePreferences.setBookmarks(grabbedID);
         Navigator.push(
             context,
             MaterialPageRoute(
               builder: (context) =>
                   bookmarkPage(bookmarkedID: widget.ListingID),
             ));
       }
     });
   }

//My button for bookmarks
FavoriteButton(
                           iconDisabledColor: Colors.grey,
                           iconSize: 50,
                           isFavorite: isBookmarked,
                           valueChanged: (_) {
                             toggleBookmark();
                           },
                         ),

bookmarks.dart

class bookmarkPage extends StatefulWidget {
 final int bookmarkedID;

 const bookmarkPage({
   Key? key,
   required this.bookmarkedID,
 }) : super(key: key);

//rest of code...
//This grabs the saved bookmark and works on app restart.
Text(UserSimplePreferences.getBookmarks() ?? ''),


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文