我没有错误,但数据没有保留,也许我以错误的方式使用它,请花点时间帮助我。请。!
@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(),
这是我用来存储数据的函数
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;
}
// 获取文件
Future<File> getFileListPreference(File file) async {
_prefs = await SharedPreferences.getInstance();
_prefs.getString(savepdf);
return file;
}
在此先感谢您的支持对我非常有用。
错误:类型 'List<Object?>' 不是类型 'String?' 的子类型 在类型转换中