skip to Main Content

I’m doing a simple check in Flutter, where after authenticating, it should redirect to HomePage().

I debugged and after authentication authController.isAuth = true but it doesn’t refer to HomePage().

class AuthOrHomePage extends StatelessWidget {
  const AuthOrHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    AuthController authController = Provider.of(context);

    return FutureBuilder(
      future: authController.tryAutoLogin(),
      builder: (ctx, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        } else if (snapshot.error != null) {
          return const Center(
            child: Text('Ocorreu um erro!'),
          );
        } else {
          if (authController.isAuth) {
            return const HomePage();
          } else {
            return const InitPage();
          }
        }
      },
    );
  }
}

Any idea what it could be?

2

Answers


  1. In flutter, if you want to navigate from one screen to another you can use Navigator.

    class AuthOrHomePage extends StatelessWidget {
      const AuthOrHomePage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        AuthController authController = Provider.of(context);
    
        return FutureBuilder(
          future: authController.tryAutoLogin(),
          builder: (ctx, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Center(child: CircularProgressIndicator());
            } else if (snapshot.error != null) {
              return const Center(
                child: Text('Ocorreu um erro!'),
              );
            } else {
              if (authController.isAuth) {
                Navigator.of(context).push(MaterialPageRoute(builder: (_) => const HomePage()));
              } else {
                Navigator.of(context).push(MaterialPageRoute(builder: (_) => const InitPage()));
              }
              return SizedBox();
            }
          },
        );
      }
    }
    

    For more : https://api.flutter.dev/flutter/widgets/Navigator-class.html#:~:text=In%20Flutter%20these%20elements%20are,pop.

    Login or Signup to reply.
  2. Try this way,

    import 'package:flutter/material.dart';
    import 'package:provider/provider.dart';
    
    class AuthOrHomePage extends StatelessWidget {
      const AuthOrHomePage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        AuthController authController = Provider.of(context);
    
        return FutureBuilder(
          future: authController.tryAutoLogin(),
          builder: (ctx, snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Center(child: CircularProgressIndicator());
            } else if (snapshot.error != null) {
              return const Center(
                child: Text('Ocorreu um erro!'),
              );
            } else {
              if (authController.isAuth) {
                // Navigate to the HomePage
                WidgetsBinding.instance!.addPostFrameCallback((_) {
                  Navigator.pushReplacement(
                    context,
                    MaterialPageRoute(builder: (context) => const HomePage()),
                  );
                });
              } else {
                return const InitPage();
              }
            }
            return const SizedBox.shrink();
          },
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search