0
votes

I am working on a web app in which, I want to keep user logged in during page refresh or even if the tab is no longer exist just like youtube/gmail web app. How to create that ???

In flutter mobile for android/ios we can used shared preferences which uses shared preferences for android and NSUserDefaults for ios to make that functionality. But in the case of Flutter web we can't store user creds into local/ session storage or even in cookies.

I recently started using flutter web so, please guide me on the right path, any link to good resources is much appreciated.

2
You, can use share preference. when you login. then set value when logout then reset value/ change value, stackoverflow.com/questions/54377188/…Md Omor Faruqe
Don't store user creds into local/ session storage. Store user access token to SharedPreferences.ישו אוהב אותך

2 Answers

1
votes

Make sure WidgetFlutterBinding.ensureInitialized() is the first line of main()

import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool login = prefs.getBool("login");
  print("login:" + login.toString());
  runApp(MaterialApp(home: login == null ? LoginPage(title: 'My App') : HomePage()));
}

class LoginPage extends StatelessWidget { ...

in details How to use shared preferences to keep user logged in flutter?

0
votes

Edited: You can use hivedb packages from pub.dev for safe and high speed storage supporting on web also:

import 'package:hive/hive.dart';
import 'dart:convert';

void main() async {
  //Hive.init('somePath') -> not needed in browser

 var box = await Hive.openBox('testBox');

 box.put('user', json.encode(User(name:'john',password:'pass')));

 print('Name: ${box.get('user')}');
}
  1. https://pub.dev/packages/hive
  2. https://docs.hivedb.dev/#/