'flutter How to persist favorite list file using Shared_preferences?
I don't have an error but the data is not persisted maybe I'm using it in the wrong way please take some time to help me. please.!
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF6F4ED),
appBar: AppBar(
actions: [
Badge(
badgeContent: Text('${savedWords.length}'),
toAnimate: true,
position: const BadgePosition(top: 0, end: 0),
child: IconButton(
icon: const Icon(Icons.bookmark),
onPressed: () => pushToFavoriteWordsRoute(context),
),
),
],
centerTitle: true,
title: const Text('Documents'),
),
body: loading
? const Center(child: CircularProgressIndicator())
: _exfilePath.isEmpty
? const Center(child: Text('No documents...'))
: GridView.builder(
shrinkWrap: true,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: _exfilePath.length,
itemBuilder: (context, int index) {
var name = _exPath[index];
var fileIndex = _exfilePath[index];
bool isSave = savedWords.contains(fileIndex);
return Padding(
padding: const EdgeInsets.all(8.0),
child: GridTile(
header: Align(
alignment: Alignment.topRight,
child: PopupMenuButton<IconTextMenu>(
color: Colors.purple[50],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16)),
onSelected: (value) async {
switch (value) {
case IconMenu.favorite:
var persistFile =
await getFileListPreference(fileIndex);
setState(() {
if (isSave) {
savedWords.remove(persistFile);
} else {
savedWords.add(persistFile);
}
});
break;
case IconMenu.delete:
_exfilePath[index].delete();
refresh();
break;
case IconMenu.share:
break;
default:
}
},
itemBuilder: (context) => IconMenu.items
.map((e) => PopupMenuItem<IconTextMenu>(
value: e,
child: ListTile(
leading: Icon(
e.icon,
color: isSave
? Colors.red
: Colors.blueAccent,
),
title: Text(e.text))))
.toList(),
here are the functions I use to store the data
Future<File> setFileListPreference(File file) async {
File f = File('');
_prefs = await SharedPreferences.getInstance();
for (File file in savedWords) {
String stringfile = file.path;
_prefs.setString(savepdf, stringfile);
File convertToFile = File(stringfile);
f = convertToFile;
}
return f = file;
}
// GET FILE
Future<File> getFileListPreference(File file) async {
_prefs = await SharedPreferences.getInstance();
_prefs.getString(savepdf);
return file;
}
your support will be very useful to me thanks in advance.
error: type 'List<Object?>' is not a subtype of type 'String?' in type cast
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
