0

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() ?? ''),


  • Try [hive](https://pub.dev/packages/hive) package – Kahou Jun 03 '22 at 23:12
  • Does this answer your question? [How to save List to SharedPreferences in Flutter?](https://stackoverflow.com/questions/61316208/how-to-save-listobject-to-sharedpreferences-in-flutter) – Kahou Jun 03 '22 at 23:15
  • That thread you linked looks promising, I'll give it a shot once I get back home. Thanks. – BedlessSleeper Jun 03 '22 at 23:34

0 Answers0