dart - 如何在 Flutter 应用程序启动时设置数据库?

标签 dart flutter

我想在我的应用程序启动时尽快在屏幕上显示一个小部件。在向用户显示小部件后,我想在通过按下另一个屏幕离开启动屏幕之前进行一些应用程序初始化(例如设置数据库)。我无法弄清楚将我的应用程序初始化代码放在哪里。类似于 iOS 上的 viewDidAppear

这是我基于initState尝试的。

class Launch extends StatefulWidget {
  @override
  _LaunchState createState() {
    return _LaunchState();
  }
}

class _LaunchState extends State<Launch> {
  @override
  Widget build(final BuildContext context) {
    print('LaunchState build start');
    final Widget w = Center(
      child: Text('Launching...'),
    );
    print('LaunchState build end');
    return w;
  }

  @override
  void initState() {
    print('LaunchState initState start');
    super.initState();
    print('LaunchState initState middle');
    _appInitialization();
    print('LaunchState initState end');
  }

  void _appInitialization() {
    print('LaunchState _appInitialization');
  }

}

输出是

flutter: LaunchState initState start
flutter: LaunchState initState middle
flutter: LaunchState _appInitialization
flutter: LaunchState initState end
flutter: LaunchState build start
flutter: LaunchState build end

我想我想看到的输出是

flutter: LaunchState build start
flutter: LaunchState build end
flutter: LaunchState initState start
flutter: LaunchState initState middle
flutter: LaunchState _appInitialization
flutter: LaunchState initState end

也许我会以一种非常稳健的方式来解决这个问题。如果有完全不同的方法,我会洗耳恭听。

解决方案

感谢 Mangaldeep Pannu 的回答,看来我缺少 async

class Launch extends StatefulWidget {
  @override
  _LaunchState createState() {
    return _LaunchState();
  }
}

class _LaunchState extends State<Launch> {
  @override
  Widget build(final BuildContext context) {
    print('LaunchState build start');
    final Widget w = Center(
      child: Text('Launching...'),
    );
    print('LaunchState build end');
    return w;
  }

  @override
  void initState() {
    print('LaunchState initState start');
    super.initState();
    print('LaunchState initState middle');
    _appInitialization(); // no await
    print('LaunchState initState end');
  }

  void _appInitialization() async {
    print('LaunchState _appInitialization begin');
    // simulate some time consuming initialization task
    await Future.delayed(Duration(seconds: 5));
    print('LaunchState _appInitialization middle');
    Navigator.push(...);
    print('LaunchState _appInitialization end');
  }

}

结果是

flutter: LaunchState initState start
flutter: LaunchState initState middle
flutter: LaunchState _appInitialization begin
flutter: LaunchState initState end
flutter: LaunchState build start
flutter: LaunchState build end
[5 second pause]
flutter: LaunchState _appInitialization middle
flutter: LaunchState _appInitialization end

最佳答案

在应用启动时打开的第一个小部件中

@override
void initState() {
  onStart();
}

void onStart() async {
  await loadData();  //load your data here
  Navigator.push();  //push to next screen
}

onStart 将加载数据异步
当数据加载时推送到下一个屏幕。

关于dart - 如何在 Flutter 应用程序启动时设置数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55671692/

相关文章:

email - 使用 Flutter 的电子邮件进行 Firebase 身份验证

dart - 按顺序断言 Stream 发出的所有项目,直到在 Dart 中取消为止?

flutter - Flutter web 应该使用 Wasm 而不是 dart2js

flutter - "setState() or markNeedsBuild() called during build"尝试在消费者小部件(提供程序包)内的导航器中推送替换时出错

flutter - 如何在 flutter 中完成当前 View /事件?

firebase - Flutter Firebase 实时数据库一次写入/更新到多个节点

flutter - unselectedLabelstyle 在 Flutter 中不起作用

android - 来自集成测试的 flutter 位置权限不起作用

dart - 实时 flutter

flutter - 导航到新屏幕时如何保持 AppBar 和后退箭头固定