【问题标题】:How to check first time app launch in Flutter如何在 Flutter 中检查应用程序的首次启动
【发布时间】:2019-12-03 17:11:50
【问题描述】:

我是一个初学者,我已经创建了我的应用程序,但我想检查用户是否在安装后第一次打开应用程序,我见过this article,但不知道怎么做?

这是启动屏幕代码,代码在 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,
          )),
        ],
      ),
    );
  }
}

【问题讨论】:

  • 您链接的答案实际上很好地解释了它。你需要了解SharedPreferencesSharedPreferences 是您的应用程序的本地存储,您可以在其中保存少量的简单数据。在您的情况下,如果用户已经打开您的应用程序,您可以使用它来存储信息。您可以在每次应用启动时检查此信息。如果信息不存在,您的用户将首次打开它。然后,您可以保存一个值并将他发送到欢迎屏幕。

标签: flutter dart launch launching-application


【解决方案1】:

这里的代码没有任何错误并且澄清得很好

class _SplashScreenState extends State<SplashScreen> {

  Future<bool> isFirstTime() async {
     var isFirstTime = SharedPref.pref.getBool('first_time');
     if (isFirstTime != null && !isFirstTime) {
       SharedPref.pref.setBool('first_time', false);
       return false;
     } else {
       SharedPref.pref.setBool('first_time', false);
       return true;
     }
  }

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

    Timer(Duration(seconds: 3), () {
    isFirstTime().then((isFirstTime) {
      isFirstTime ? print("First time") : print("Not first time");
     });
    }
   );
  }

}

【讨论】:

  • 您使用哪个包进行共享首选项访问
  • 使用了他们的 shared_preferences:^0.5.7+1
【解决方案2】:

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

  • 依赖pubspec.yaml 中的shared_preferences 包并运行Packages get
dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^0.5.4+6
  • 导入包:
import 'package:shared_preferences/shared_preferences.dart';
  • 实现它:
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');
  }
  ........

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

希望这会有所帮助。

【讨论】:

  • 谢谢,这对我有帮助
  • @SardorbekRkh 我认为计时器与 OP 的应用程序功能有关。问题中提到了。
【解决方案3】:

使用is_first_run 包更简单。你只需这样做:

bool firstRun = await IsFirstRun.isFirstRun();

如果应用是第一次启动,则返回true

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多