0

クラスでデータを渡し、共有設定でマップに保存したいと考えています。別のクラスでデータを読み取ります。私の問題は、マップを保存してデータを複数回渡すことです。どうすればマップのリストをアプリに保存できますか?

これは私のコードですが、間違っていると思います:

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();
            },
          );
  }
}

誰でも私を助けてくれますか?

4

1 に答える 1