0

I need to redirect user to login page when he clicks on logout button from drawer (wherever he is). The problem is that when I click on the logout button, the screen remains the same.

According to this post: Flutter provider state management, logout concept

I have:

void main() async {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider<Profile>(
          create: (final BuildContext context) {
            return Profile();
          },
        )
      ],
      child: MyApp(),
    ),
  );
}

MyApp:

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  /// Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    if (!mounted) return;
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        initialRoute: '/',
        navigatorKey: navigatorKey,
        // ...
        home: Consumer<Profile>(
          builder: (context, profile, child){
             return profile.isAuthenticated ? SplashScreen() : AuthScreen();
          }
        )
    );
  }
}

The part of the drawer where there is the logout button:

ListTile(
    leading: Icon(Icons.logout),
    title: Text(AppLocalizations.of(context)!.logout),
    onTap: () async {
      SharedPreferences preferences =
          await SharedPreferences.getInstance();
      await preferences.clear();

      final Profile profile =
          Provider.of<Profile>(context, listen: false);
      profile.isAuthenticated = false;
    }),

As I said, when I click on the logout button from the drawer, the user is correctly logged out, but the screen remains the same.

UPDATE

This is the profile class:

class Profile with ChangeNotifier {
  bool _isAuthenticated = false;

  bool get isAuthenticated {
    return this._isAuthenticated;
  }

  set isAuthenticated(bool newVal) {
    this._isAuthenticated = newVal;
    this.notifyListeners();
  }
}
Giacomo M
  • 4,450
  • 7
  • 28
  • 57

3 Answers3

0

Add Navigator.of(context).pushReplacementNamed("/routeName") in LogOut onTap() Section.

For more information : https://api.flutter.dev/flutter/widgets/Navigator/pushReplacementNamed.html

Make sure to have logout route set in MyApp file, and i'd edit logout button file as such:

ListTile(
    leading: Icon(Icons.logout),
    title: Text(AppLocalizations.of(context)!.logout),
    onTap: () async {
      SharedPreferences preferences =
          await SharedPreferences.getInstance();
      await preferences.clear();

      final Profile profile =
          Provider.of<Profile>(context, listen: false);
      profile.isAuthenticated = false;

      // add login file route here using Navigator.pushReplacementNamed() ; 
    }),
Yashraj
  • 1,025
  • 1
  • 5
  • 22
Delwinn
  • 891
  • 4
  • 19
  • What you mean with `Make sure to have logout route set in MyApp file`? And, I could put `Navigator.pushReplacementNamed`, but I use provider to avoid that part of code. The provider should do this job for me. – Giacomo M Sep 17 '21 at 08:20
  • Why not check out the this video by TheNetNinja, https://youtu.be/j_SJ7XmT2MM – Delwinn Sep 18 '21 at 09:22
0

I think you are using provider class incorrectly. use your profile class like this.

    class Profile with ChangeNotifier {
           bool _isAuthenticated = true;

           bool get getIsAuthenticated => _isAuthenticated;

           set setIsAuthenticated(bool isAuthenticated) {
                  _isAuthenticated = isAuthenticated;
                 notifyListeners();//you must call this method to inform lisners
                }
}

in set method call notifyListners();

in your listTile replace profile.isAuthenticated = false to profile.isAuthenticated = false; Always use getters and setters for best practice. I hope this is what you were looking for.

-1

Navigator push named -> logout route?

Kozubi
  • 529
  • 3
  • 12