如何检查用户是否首次打开我的应用(Flutter应用,飞镖代码) [英] How to check If user open my app first time (Flutter App,Dart Code)

查看:905
本文介绍了如何检查用户是否首次打开我的应用(Flutter应用,飞镖代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者,我创建了我的应用程序,但是我想检查用户是否在安装后第一次打开该应用程序,我看到了

I am a beginner in flutter, I have created my application but I want to check if the user opens the application for the first time after installing, I have seen this article but did not know how that?

这是启动屏幕代码,该代码会在3秒钟后将用户直接移动到主屏幕,但是我想检查用户是否是第一次打开应用并将用户移动到欢迎"屏幕,或者是否不是用户第一次将用户移动到主屏幕.屏幕.

This is the splash screen code, the code move the user directly to Main screen after 3 sec, But I want check if user first time open app and move user to Welcome screen or if user not first time and move user to Main screen.

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,
          )),
        ],
      ),
    );
  }
}

推荐答案

@Abdullrahman,请使用其他人建议的shared_preferences.这是您的方法,

@Abdullrahman, please use shared_preferences as suggested by others. Here is how you can do that,

  • 取决于pubspec.yaml中的shared_preferences程序包并运行Packages get:
  • Depend on shared_preferences package in pubspec.yaml and run 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可能会显得过大.

      Note: SharedPreferences data will be removed if user clears the cache. SharePreferences is a local option. If you want to prevent that, you can use firestore to save bool value but firestore would probably be an overkill for a simple task like this.

      希望这会有所帮助.

      这篇关于如何检查用户是否首次打开我的应用(Flutter应用,飞镖代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆