How to check If user open my app first time (Flutt

2020-06-24 16:17发布

问题:

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?

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

回答1:

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

  • Depend on shared_preferences package in pubspec.yaml and run Packages get:
dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^0.5.4+6
  • Import the package:
import 'package:shared_preferences/shared_preferences.dart';
  • Implement it:
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');
  }
  ........

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.

Hope this helps.



回答2:

You can use https://pub.dev/packages/shared_preferences add a value the first time a user enters



回答3:

Here are code of without any error and clarifying well

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");
     });
    }
   );
  }

}