Septatrivanto Wandy
Septatrivanto Wandy

Reputation: 57

App already has local storage (sharedPrefrences) on fresh install flutter

I have this class rendered when the first time lauching an app

import 'package:distributor_obat_semarang/screens/login.dart';
import 'package:distributor_obat_semarang/screens/splash.dart';
import 'package:distributor_obat_semarang/screens/tabs.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AuthScreen extends StatefulWidget {
  const AuthScreen({super.key});

  @override
  State<AuthScreen> createState() => _CheckAuthScreenState();
}

class _CheckAuthScreenState extends State<AuthScreen> {
  final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  late Future<bool> loginCheckFuture;

  Future<bool> _checkIfLoggedIn() async {
    final SharedPreferences prefs = await _prefs;
    var isAuth = prefs.getString('username');
    if (isAuth != null) {
      return true;
    }
    return false;
  }

  @override
  void initState() {
    super.initState();
    loginCheckFuture = _checkIfLoggedIn();
  }

  @override
  Widget build(BuildContext context) {
    Widget child;
    return FutureBuilder(
      future: loginCheckFuture,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          if (snapshot.data == true) {
            child = const TabsScreen();
          } else {
            child = const LoginScreen();
          }
        } else {
          child = const SplashScreen();
        }
        return Scaffold(
          body: child,
        );
      },
    );
  }
}

The _checkIfLogin function is using sharedPreferences to check value if there is a "username" key stored in the localStorage. If isAuth value is not null, then the screen will be push to TabsScreen(), if it is null, then the screen will be push to LoginScreen. That is what i expect the result

But reality is when I build apk and fresh installed on a real device, the app has already stored "userName" key in the local storage, makes the screen push directly to TabsScreen.

When I run it on simulator and emulator on fresh installed, it works perfectly without any stored data in localStorage, but when I build app-release.apk and fresh installed on a real device, app has already stored "username" key to a local storage.

Then when I clear data app manually from real device settings app, the app now runs perfectly, but uninstall and install again (reinstall) it happen again.

Can someone help me to solve this? I already did flutter clean, restart IDE, restart my laptop, close all simulator emulator, the bug still occur.

Update: I have 2 android devices that installed this app, after reinstalled many times and I tried installing app-debug.apk, my 1st device fix it self and works perfectly, but my 2nd device still bugging. Is the problem about real devices it self that address to localStorage?

Here is my AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:label="Distributor Obat Semarang"
        android:name="${applicationName}"
        android:icon="@mipmap/launcher_icon">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

Upvotes: 2

Views: 104

Answers (0)

Related Questions