在页面加载之前,我该怎么做才能在flutter中悬挂一些数据

发布于 2025-02-12 09:27:26 字数 729 浏览 2 评论 0原文

我正在写一个Simeple Todo List应用程序,该应用程序存储在sqlite中 SQFlite: ^2.0.0+3立即。我想在加载“颤音”页面之前加载sqlite todo数据,这是我的初始代码在扑朔迷离中看起来像:

class _HomePageState extends State<HomePage> {
  GlobalKey _inputViewKey = GlobalKey();
  List<Todo> _todos = [];

  @override
  void initState() {
    var _db = DBProvider.db;
    _todos = await _db.getAllTodo();
    super.initState();
  }
}

这是从数据库加载数据的功能:

 Future<List<Todo>> getAllTodo() async {
    final db = await database;
    var result = await db.query('Todo');
    return result.map((it) => Todo.fromJson(it)).toList();
  }

iDE告诉我应该在初始函数中添加异步。当我添加异步函数时,初始函数无法正常工作。我该怎么做才能使它起作用?如何在首页之前初始化异步数据?

I am writing a simeple todo list app, the todo item stored in sqlite
sqflite: ^2.0.0+3 right now. I want to load the sqlite todo data before loading the flutter page, this is my initial code looks like in flutter:

class _HomePageState extends State<HomePage> {
  GlobalKey _inputViewKey = GlobalKey();
  List<Todo> _todos = [];

  @override
  void initState() {
    var _db = DBProvider.db;
    _todos = await _db.getAllTodo();
    super.initState();
  }
}

and this is the function to load data from database:

 Future<List<Todo>> getAllTodo() async {
    final db = await database;
    var result = await db.query('Todo');
    return result.map((it) => Todo.fromJson(it)).toList();
  }

the IDE told that I should add async in the initial function. When I add the async function, the initial function could not work. What should I do to make it work? how to initial the async data before the HomePage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漆黑的白昼 2025-02-19 09:27:26

您不能在无数上标记异步。您可以尝试

class _HomePageState extends State<HomePage> {
  GlobalKey _inputViewKey = GlobalKey();
  List<Todo> _todos = [];

  @override
  void initState() {
var _db = DBProvider.db;
getAllTodo();
    super.initState();
  }
}

并在方法中

getAllTodo() async {
    final db = await database;
    var result = await db.query('Todo');
    _todos = result.map((it) => Todo.fromJson(it)).toList();
  setState((){});
  }

You cant mark async on inistate. You can try this

class _HomePageState extends State<HomePage> {
  GlobalKey _inputViewKey = GlobalKey();
  List<Todo> _todos = [];

  @override
  void initState() {
var _db = DBProvider.db;
getAllTodo();
    super.initState();
  }
}

And in the method

getAllTodo() async {
    final db = await database;
    var result = await db.query('Todo');
    _todos = result.map((it) => Todo.fromJson(it)).toList();
  setState((){});
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文