Splashscreen with named routes

Asked

Viewed 35 times

0

I’m using the Splashscreen package in my app. I want the splashscreen screen the app identify that there is already a logged in user that was done with firebase. However, along with the route call it is necessary to pass argument with the data of the logged-in user caught in the controller, but the splashscreen navigateAfterSeconds accepts only String or Widget. How do I call the route next to the argument on this splashscreen?

SplashScreen(
          seconds: 5,
          gradientBackground: LinearGradient(
            begin: Alignment.topRight,
            end: Alignment.bottomLeft, 
            colors: [Colors.blue, Colors.blueAccent],
          ),
          navigateAfterSeconds: // AQUI VAI O RETORNO DA FUNÇÃO ISLOGGED, 
          loaderColor: Colors.transparent,
        ),

class InitialController extends GetxController {
  final box = GetStorage('habito_invest_app');

  
  // Verifica se já existe usuário logado
  dynamic isLogged() async {
    if(box.hasData('auth')){
      UserModel user = UserModel(
        id: box.read('auth')['id'],
        email: box.read('auth')['email'],
        name: box.read('auth')['name'],
        urlimage: box.read('auth')['urlimage']
      );
      return Routes.HOME; //preciso passar o user junto
    } else {
      return Routes.LOGIN;
    }
  }

}

2 answers

0


I solved the problem by leaving the package and implementing the onReady function in the Controller, so I could set the user on the route, something the package did not leave. That way it worked perfectly.

class SplashScreenController extends GetxController {
  final box = GetStorage('habito_invest_app');

@override
void onReady() {
 Timer(
   Duration(seconds: 3),
   () => isLogged()
 );
 super.onReady();
}

// Verifica se já existe usuário logado
void isLogged(){     
if(box.hasData('auth')){
  UserModel user = UserModel(
    id: box.read('auth')['id'],
    email: box.read('auth')['email'],
    name: box.read('auth')['name'],
    urlimage: box.read('auth')['urlimage']
  );
  Get.offAllNamed(Routes.HOME, arguments: user);
  } else {
    Get.offAllNamed(Routes.LOGIN);
  }
 }
}

0

You could create a Singleton from a Usercontroller that has a User object, which you could retrieve in other parts of your app and use it to set and retrieve it in your Home.

Usercontroller { User user = User();

// user methods....

}

main (){ Get.put(Usercontroller()); }

Final Usercontroller userController = Get find();

userController.user = Userfromstorage();

Return Routes.Home;

And at Home you can recover user data so:

Final Usercontroller userController = Get find();

Text(userController.user.name)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.