根据用户是否登录返回不同的flutter widget

问题描述 投票:0回答:1

我是 Flutter 新手,希望根据用户是否登录返回登录屏幕或主屏幕。我使用 auth0 进行身份验证,并使用提供程序作为应用程序状态管理解决方案。

我尝试使用 context.watch 但无法使其工作。

flutter auth0
1个回答
0
投票

您可以检查用户是否登录,此流程的一般实现是将令牌存储在持久存储中,但在您的情况下,AuthO 自己在

Credentials? _credentials; 

中提供数据/持久令牌

这是实现

//Declare the variables
Credentials? _credentials;
  late Auth0 auth0;
  
  ...
  
// Initialize them
  void initState() {
    super.initState();
    auth0 = Auth0('{domain}', '{clientId}');
    errorMessage = '';
  }

  ...

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Auth0 Demo',
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Auth0 Demo'),
        ),
        body: Center(
          child: isBusy
              ? const CircularProgressIndicator()
              : _credentials != null
                  ? Profile(logoutAction, _credentials?.user)
                  : Login(loginAction, errorMessage),
        ),
      ),
    );
  }
© www.soinside.com 2019 - 2024. All rights reserved.