Flutter Show Separate page based on FireStore data field

I have a basic App where I use Email SignIn and SignUp to login to the app. I use a Provider and I’m pushing to either a Login page or a Home page based on the users current state.

When the user Logs In, I want to redirect him to a New Screen (info Page) where he has to fill some other data, which will be saved in Firestore under a document as his UID. (these extra data are just Address/Names/phone… bla bla)

But, if the user logs out and Logs in again, He shouldn’t be redirected to the info Page since he has already submitted that data.

How can I redirect a user if he has NOT submitted the data to the info page and if he has, how can I send him to the Home Page?

This is my Code with Provider and HomeController

@override
  Widget build(BuildContext context) {
    return Provider(
      auth: AuthService(),
      db: FirebaseFirestore.instance,
      child: MaterialApp(
          theme: ThemeData(
            primaryColor: Colors.lightGreen[300],
            primaryColorDark: Colors.amber,
            errorColor: Colors.amber,
            hintColor: Colors.grey
          ),
          title: "Farmline.LK",
          home: HomeController(),
          routes: <String, WidgetBuilder>{
            '/home': (BuildContext context) => HomeController(),
            '/signUp': (BuildContext context) => FirstView(),
          }),
    );
  }
}
class HomeController extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final AuthService auth = Provider.of(context).auth;
    return StreamBuilder<String>(
      stream: auth.onAuthStateChanged,
      builder: (context, AsyncSnapshot<String> snapshot) {
        if (snapshot.connectionState == ConnectionState.active) {
          final bool signedIn = snapshot.hasData;
          return signedIn ? Home() : FirstView();
        }
        return Container();
      },
    );
  }
}

Thank you so much for your support!

Heyy! @KAD9_911 The best place to check this would be the splash screen. Here in initstate you can call a function that checks two things:

Case 1: User has logged in and then closed the app.

In this case you should store his UID or the key that you use to authenticate the user in shared preferences. On splash screen you can check whether this value is null or not. If null then you can redirect to login screen. If not null then you can go to the second check ------>

Case 2: User is logged in but has not filled the info

This user will pass the first check, then you will have to check whether all required data is present. You can do this via a getUserInfo() api call. And take him to the information form. Else to home screen.

class SplashScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SplashScreenState();
  }
}

class _SplashScreenState extends State<SplashScreen> {
     @override
  void initState() {
      super.initState();
      goToNextScreen();
   }

void  goToNextScreen() {
    Future.delayed(Duration(seconds: 3), () async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      dynamic isAuthenticated =
          prefs.getBool('isAuthenticated') ?? false; //null safety
      if (!isAuthenticated) {
        Navigator.of(context).pushReplacement(MaterialPageRoute(
          builder: (context) => LoginScreen(),
        ));
      } else {
        bool infoPresent = await checkInfoPresent();
        if (infoPresent) {
          Navigator.of(context).pushReplacement(
              MaterialPageRoute(builder: (context) => HomeScreen()));
        } else {
          Future.delayed(Duration(seconds: 3), () async {
            Navigator.of(context).pushReplacement(
                MaterialPageRoute(builder: (context) => InformationForm()));
          });
        }
      }
    });
  }

Hope this helps :slight_smile: