我想在我的班级中传递数据并使用共享偏好将它们保存在地图上。并在另一个班级阅读我的数据。我的问题是关于保存地图和多次传递数据。那么如何在我的应用程序中保存地图列表?
这是我的代码,但我认为它是错误的:
class DownloadAudio extends StatefulWidget {
final String audioUrl;
final String imageUrl;
final String title;
const DownloadAudio(
{Key? key,
required this.audioUrl,
required this.imageUrl,
required this.title})
: super(key: key);
@override
_DownloadAudioState createState() => _DownloadAudioState();
}
class _DownloadAudioState extends State<DownloadAudio> {
Map<String,String> episodesData = {
"image" : "",
"audio" : "",
"title" : ""
}
;
saveEpisodesList(Map episodesList) async {
SharedPreferences pathEpisodesList = await SharedPreferences.getInstance();
Map<String, dynamic> json = {'list': episodesList};
pathEpisodesList.setString('EpisodesList', jsonEncode(json));
episodesData = await getEpisodesList();
// return episodesData;
}
getEpisodesList() async {
SharedPreferences pathEpisodesList = await SharedPreferences.getInstance();
if (pathEpisodesList.getString('EpisodesList') == "[]") {
} else {
episodesData = await json
.decode(pathEpisodesList.getString('EpisodesList')!)['list'];
return episodesData;
}
}
@override
Widget build(BuildContext context) {
Map<String,String> episodesList = {
"image" : widget.title ,
"audio" : widget.audioUrl ,
"title" : widget.imageUrl ,
};
saveEpisodesList(episodesList);
return IconButton(
icon: SvgPicture.asset(MyIcons.frame),
onPressed:() async{
downloadFile();
},
);
}
}
任何人都可以帮助我吗?