如何在 Flutter 中检查应用程序的首次启动时间

Abd*_*sfi 5 launch launching-application dart flutter

我是 flutter 的初学者,我已经创建了我的应用程序,但我想检查用户安装后是否第一次打开应用程序,我看过这篇文章但不知道怎么做?

这是初始屏幕代码,该代码在 3 秒后将用户直接移动到主屏幕,但我想检查用户是否第一次打开应用程序并将用户移动到欢迎屏幕,或者用户是否不是第一次并移动用户到主屏幕。

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:book_pen/main.dart';
import 'package:book_pen/Welcome.dart';

void main() {
  runApp(new MaterialApp(
    home: new SplashScreen(),
    routes: <String, WidgetBuilder>{
      '/HomePage': (BuildContext context) => new HomePage(),
      '/WelcomePage': (BuildContext context) => new WelcomePage()
    },
  ));
}

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => new _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  startTime() async {
    var _duration = new Duration(seconds: 3);

    return new Timer(_duration, navigationPageHome);
  }

  void navigationPageHome() {
    Navigator.of(context).pushReplacementNamed('/HomePage');
  }

  void navigationPageWel() {
    Navigator.of(context).pushReplacementNamed('/WelcomePage');
  }

  @override
  void initState() {
    super.initState();
    startTime();
  }

@override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Center(
            child: new Image.asset(
              'assets/images/SplashBack.jpg',
              width: size.width,
              height: size.height,
              fit: BoxFit.fill,
            ),
          ),
          Center(
              child: new Image.asset(
            'assets/images/BigPurppleSecSh.png',
            height: 150,
            width: 300,
          )),
        ],
      ),
    );
  }
}

Run Code Online (Sandbox Code Playgroud)

Pro*_*Pro 7

@Abdullrahman,请shared_preferences按照其他人的建议使用。这是你可以这样做的方法,

  • 依赖于 shared_preferences 包pubspec.yaml并运行Packages get
dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^0.5.4+6
Run Code Online (Sandbox Code Playgroud)
  • 导入包:
import 'package:shared_preferences/shared_preferences.dart';
Run Code Online (Sandbox Code Playgroud)
  • 实施它:
class _SplashScreenState extends State<SplashScreen> {
  startTime() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool firstTime = prefs.getBool('first_time');

    var _duration = new Duration(seconds: 3);

    if (firstTime != null && !firstTime) {// Not first time
      return new Timer(_duration, navigationPageHome);
    } else {// First time
      prefs.setBool('first_time', false);
      return new Timer(_duration, navigationPageWel);
    }
  }

  void navigationPageHome() {
    Navigator.of(context).pushReplacementNamed('/HomePage');
  }

  void navigationPageWel() {

    Navigator.of(context).pushReplacementNamed('/WelcomePage');
  }
  ........
Run Code Online (Sandbox Code Playgroud)

注意:如果用户清除缓存,SharedPreferences 数据将被删除。SharePreferences 是一个本地选项。如果您想防止这种情况发生,您可以使用 firestore 来保存 bool 值,但对于像这样的简单任务来说,firestore 可能是一种矫枉过正。

希望这可以帮助。


Erd*_*ray 6

使用is_first_run包就更简单了。您只需执行以下操作:

bool firstRun = await IsFirstRun.isFirstRun();
Run Code Online (Sandbox Code Playgroud)

true如果应用程序是第一次启动,则会返回。


Cen*_*MUR 3

您可以使用https://pub.dev/packages/shared_preferences在用户第一次输入时添加一个值