2

I need when the user open the application, the splash screen opens. The splash screen decided if the user is gonna go to home page or sign in, using middleware in GetX layer.

this function:

  @override
  RouteSettings? redirect(String? route) async{ // -> error
 GetSignedInUserInfoRepoImp _sigenedInUser = GetSignedInUserInfoRepoImp();
    if ( await _sigenedInUser.isUserSignedIn()) {
      return const RouteSettings(name:  Routes.HOME);
    } else {
      return const RouteSettings(name:  Routes.SIGN_IN);
    }
  }

but i can not use redirect function with future function. what should i do?

3 Answers3

1

You should return GetMaterialApp.router() instead of GetMaterialApp() in your main() build function and then use the snippet code below based on your example :

@override
  Future<GetNavConfig?> redirectDelegate(GetNavConfig route) async {
    var _sigenedInUser = Get.find<GetSignedInUserInfoRepoImp>();
    bool _isSignedIn = await _sigenedInUser.isUserSignedIn();
    if (_isSignedIn) {
      return Get.rootDelegate.toNamed('/home');
    }else {
      return Get.rootDelegate.toNamed('/sign_in');
    }
    return await super.redirectDelegate(route);
  }

More information here.

bond benz
  • 61
  • 1
  • 5
0

Turn GetSignedInUserInfoRepoImp into a GetxService and hold the current login status of the user in this service. Change login status according to login / logout etc.

Then, query the service from the GetMiddleware:

// SERVICE

class GetSignedInUserInfoRepoImp extends GetxService {
  final isLoggedIn = false;

  bool isUserSignedIn() { 
   return isLoggedIn;
  }
}

// MIDDLEWARE

@override
RouteSettings? redirect(String? route) { 
  var _sigenedInUser = Get.find<GetSignedInUserInfoRepoImp>();
  
  if (_sigenedInUser.isUserSignedIn()) {
    return const RouteSettings(name:  Routes.HOME);
  } else {
    return const RouteSettings(name:  Routes.SIGN_IN);
  }
}

Initialize this service via Getx-AppBinding

// BINDING

class AppBinding implements Bindings {
  @override
  void dependencies() {
    Get.put<GetSignedInUserInfoRepoImp>(GetSignedInUserInfoRepoImp());
  }
}

// APP

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

  @override
  Widget build(BuildContext context) {

    return GetMaterialApp(
      initialBinding: AppBinding(),
      home: Home(),
    );
  }  
}

https://chornthorn.github.io/getx-docs/dependency-management/binding/

Dabbel
  • 2,468
  • 1
  • 8
  • 25
-2

You can use redirectDelegate:

 @override
 Future<GetNavConfig?> redirectDelegate(GetNavConfig route) async{
   GetSignedInUserInfoRepoImp _sigenedInUser = GetSignedInUserInfoRepoImp();
  if ( await _sigenedInUser.isUserSignedIn()) {
      return const RouteSettings(name:  Routes.HOME);
  } else {
      return const RouteSettings(name:  Routes.SIGN_IN);
  }
}
S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30