【问题标题】:Flutter: Save Lists localFlutter:将列表保存在本地
【发布时间】:2026-02-08 18:35:01
【问题描述】:

。您好,我希望我的程序将我的列表保存在本地,这样当应用程序启动时,列表就会加载,并且每个新条目都会保存列表。(删除/删除功能应该将列表保存到)。我在 Internet 上搜索,但没有人以我想要的方式显示它。我希望有人可以帮助我。

编辑:我也想知道如何保存布尔值

这里是列表,下面是按钮,用于将文本字段中的输入添加到列表中

 class TimescheduleData {
  static List<String> time = [];

  static List<String> who = [];
  static List<String> when = [];
  static List<String> where = [];
} ```

TextButton(
                child: Icon(
                  Icons.check_circle_outline_rounded,
                  color: Colors.green,
                  size: 120,
                ),
                
                  TimescheduleData.time.add(myControllertime.text);
                  TimescheduleData.who.add(myControllerwho.text);
                  TimescheduleData.when.add(myControllerwhen.text);
                  TimescheduleData.where.add(myControllerwhere.text);
                  myControllertime.clear();
                  myControllerwho.clear();
                  myControllerwhen.clear();
                  myControllerwhere.clear();
                  Navigator.push(
                      context, MaterialPageRoute(builder: (context) => App()));
                })

void deleteDate(int Index) {
    setState(() {
      TimescheduleData.time.removeAt(Index);
      TimescheduleData.when.removeAt(Index);
      TimescheduleData.where.removeAt(Index);
      TimescheduleData.who.removeAt(Index);
    });
  }


GestureDetector(
  onDoubleTap: () => deleteDate(Index),

【问题讨论】:

标签: list flutter save storage


【解决方案1】:

为了在本地保存列表或任何内容,您需要某种方式来持久化数据。那里有多种选择。取决于您是需要繁重的关系模型还是只需要存储一些数据。

我以前使用过 Hive,它是一个很棒的数据库(基于键、值)。但是您也有 SQL 选项和 shared_preferences,它们也是基于键值的,但比 hive 更简单。

这是 codemagic 的一篇很棒的文章,解释了这些选项

https://blog.codemagic.io/choosing-the-right-database-for-your-flutter-app/

【讨论】:

    最近更新 更多