8

I am a beginner in a 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 the Main screen after 3 sec, But I want to check if user first time opens the app and move the user to Welcome screen or if user not the first time and move the user to the 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 Wasfi
  • 401
  • 1
  • 7
  • 13
  • 1
    The answer you linked actually explains it well. You need to understand `SharedPreferences`. The `SharedPreferences` are a local storage for your app, where you can save small amount of simple data. In your case, you can use to store the information if a user already opened your app. You can check this information on each app start. If the information isn't there, your user opens it for the first time. You can then save a value and send him to the welcome screen. – Alexander Hoffmann Dec 03 '19 at 17:38

4 Answers4

12

@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.

Pro
  • 2,843
  • 1
  • 18
  • 29
3

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

Cenk YAGMUR
  • 3,154
  • 2
  • 26
  • 42
3

It is even simpler with is_first_run package. You simply do:

bool firstRun = await IsFirstRun.isFirstRun();

It returns true if the app is launched for the first time.

Eray Erdin
  • 2,633
  • 1
  • 32
  • 66
0

You may set up a boolean during first time app is launched or installed. Once the app is launched or installed first time, set it to true. The default value should be false. After setting it to true, you must save this in the shared_preference in local storage. After that each time on you relaunch the app, read the shared_preference value. The value should be always true unless you change it. watch the video here

dylan
  • 319
  • 2
  • 4