0

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!

  • Extend your login inside builder method. Please take a look at this example: Solution 1 https://stackoverflow.com/questions/45353730/firebase-login-with-flutter-using-onauthstatechanged – manRo Oct 14 '20 at 17:44
  • I already have a working state management system. This doesnt help me get more info from firestore and redirect to another page... – Kalindu Aragorn Oct 15 '20 at 03:20

1 Answers1

0

Extend your current logic to something like this:

return StreamBuilder<String>(
  stream: auth.onAuthStateChanged,
  builder: (context, AsyncSnapshot<String> snapshot) {
    if (snapshot.connectionState == ConnectionState.active) {
      if (snahpshot.hasData) {
          var user = snapshot.data;
          if (user.name == null || user.phone == null ...) {
             return FirstView();
          } 
          return Home();
      }
      return Container();
    }
  },
);
manRo
  • 1,455
  • 1
  • 14
  • 19