skip to Main Content

I am not sure what I am doing wrong here. In the below code I am setting the initialRoute dynamically based on the session status. If a session exists then I navigate the user to the conversations widgets else to the Welcome widget. But here the user is always getting directed to the Welcome widget. I see the printing the initialRoute print the expected value. Please advise.

Here is the main fn –

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initHiveForFlutter();

  await _configureAmplify().then(
    (value) async {
      String initialRoute = await fetchAuthSession();
      print('Initial Route - $initialRoute');
      runApp(
        GraphQLProvider(
          client: GraphQLInstance().graphqlInit,
          child: MaterialApp(
            initialRoute: initialRoute,
            routes: {
              '/': (BuildContext context) => const Welcome(),
              '/authentication': (BuildContext context) =>
                  const AuthenticationController(),
              '/conversations': (BuildContext context) =>
                  const ConversationsController(),
            },
          ),
        ),
      );
    },
  );
}

Here is the fetchAuthSession fn-

Future<String> fetchAuthSession() async {
  final session = await AmplifyInstance().fetchAuthSession();
  if (session) {
    safePrint("Current User - ${AmplifyInstance().currentUser}");
    return '/conversations';
  }
  return '/';
}

2

Answers


  1. Chosen as BEST ANSWER

    This was the way I was able to do it. Setting the home attribute of the MaterialApp conditionally.

    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await initHiveForFlutter();
    
      await _configureAmplify().then(
        (value) async {
          runApp(
            GraphQLProvider(
              client: GraphQLInstance().graphqlInit,
              child: MaterialApp(
                home: await fetchAuthSession()
                    ? const ConversationsController()
                    : const Welcome(),
                routes: {
                  '/authentication': (BuildContext context) =>
                      const AuthenticationController(),
                  '/conversations': (BuildContext context) =>
                      const ConversationsController(),
                },
              ),
            ),
          );
        },
      );
    }
    

  2. The error seems to be caused by the await inside of ‘then()’ callback of future returned by the ‘_configureAmplify()’ causing await keyword not to be awaited by the ‘main()’ function itself, try this

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await initHiveForFlutter();
      
      await _configureAmplify();
    
      String initialRoute = await fetchAuthSession();
      print('Initial Route - $initialRoute');
      runApp(
        GraphQLProvider(
          client: GraphQLInstance().graphqlInit,
          child: MaterialApp(
            initialRoute: initialRoute,
            routes: {
              '/': (BuildContext context) => const Welcome(),
              '/authentication': (BuildContext context) =>
                  const AuthenticationController(),
              '/conversations': (BuildContext context) =>
                  const ConversationsController(),
            },
          ),
        ),
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search