首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Future builder内部使用本地身份验证将在身份验证后调用本地身份验证无限次

在Future builder内部使用本地身份验证将在身份验证后调用本地身份验证无限次
EN

Stack Overflow用户
提问于 2020-09-18 22:27:44
回答 2查看 134关注 0票数 0

在用户使用应用程序之前,我尝试使用本地身份验证对用户进行身份验证。但问题是,我必须使用Future Builder检查用户数据,才能进入主屏幕或登录屏幕。因此,我必须在Future Builder中使用本地身份验证来验证用户身份。但这会导致在我到达主屏幕后调用指纹身份验证无数次。所以我们不能摆脱本地身份验证。请帮帮忙,告诉我有没有别的办法。提前感谢:)

代码语言:javascript
复制
 class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      final AuthMethods _authMethods = AuthMethods();
      final LocalAuthentication _localAuthentication = LocalAuthentication();
      bool _hasFingerPrintSupport = false;
      bool _authorizedOrNot = false;
      List<BiometricType> _availableBuimetricType = List<BiometricType>();
    
      @override
      void initState() {
        super.initState();
        _getBiometricsSupport();
        _getAvailableSupport();
      }
    
      Future<void> _getBiometricsSupport() async {
        bool hasFingerPrintSupport = false;
        try {
          hasFingerPrintSupport = await _localAuthentication.canCheckBiometrics;
        } catch (e) {
          print(e);
        }
        if (!mounted) return;
        setState(() {
          _hasFingerPrintSupport = hasFingerPrintSupport;
        });
      }
        Future<void> _getAvailableSupport() async {
    
        List<BiometricType> availableBuimetricType = List<BiometricType>();
        try {
          availableBuimetricType =
              await _localAuthentication.getAvailableBiometrics();
        } catch (e) {
          print(e);
        }
        if (!mounted) return;
        setState(() {
          _availableBuimetricType = availableBuimetricType;
        });
      }
    
      Future<void> _authenticateMe() async {
     
        bool authenticated = false;
        try {
          authenticated = await _localAuthentication.authenticateWithBiometrics(
            localizedReason: "Authenticate to use App", // message for dialog
            useErrorDialogs: true,// show error in dialog
            stickyAuth: false,// native process
          );
        } catch (e) {
          print(e);
        }
        if (!mounted) return;
        setState(() {
          _authorizedOrNot = authenticated ? true : false;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        final themeNotifier = Provider.of<ThemeNotifier>(context);
      _authenticateMe();
        return MultiProvider(
          providers: [
            ChangeNotifierProvider(
              create: (_) => ThemeNotifier(darkTheme),
            ),
            ChangeNotifierProvider(create: (_) => ImageUploadProvider()),
            ChangeNotifierProvider(
              create: (_) => VideoUploadProvider(),
            ),
            ChangeNotifierProvider(create: (_) => UserProvider()),
          ],
          child: MaterialApp(
            title: "App",
            debugShowCheckedModeBanner: false,
            initialRoute: '/',
            routes: {
              '/search_screen': (context) => SearchScreen(),
              '/setting_page': (context) => settingPage(),
            },
            theme: themeNotifier.getTheme(),
            home: FutureBuilder(
              future: _authMethods.getCurrentUser(),
              builder: (context, AsyncSnapshot<User> snapshot) {
                   
                if (snapshot.hasData ) {
                   return _authorizedOrNot==true ? HomeScreen() : Container(); 
                } else {
                  return LoginScreen();
                }
              },
            ),
          ),
        );
      }
    }
EN

回答 2

Stack Overflow用户

发布于 2020-09-19 08:15:33

在这种情况下,在构建开始时调用_authenticateMe(); ()。

_authenticateMe();在setState内部导致build()再次触发并调用_authenticateMe();,从而进行重建,从而进行重建。

附注:我会将FutureBuilder向上移动,直到超过MaterialApp为止,这可能会导致使用热重新加载时出现问题。

票数 0
EN

Stack Overflow用户

发布于 2020-09-19 14:17:29

好吧,我想出了一种方法,在初始化状态下调用身份验证函数,然后在返回Future builder之前检查isauthorizedorNot。以下是代码:

代码语言:javascript
复制
class _MyAppState extends State<MyApp> {
final LocalAuthentication _localAuthentication = LocalAuthentication();
    final AuthMethods _authMethods = AuthMethods();
  bool _authorizedOrNot ;

  Future<void> _authenticateMe() async {
    bool authenticated = false;
    try {
      authenticated = await _localAuthentication.authenticateWithBiometrics(
        localizedReason: "Authenticate to use app", 
        useErrorDialogs: true,

        stickyAuth: false, 
      );
    } catch (e) {
      print(e);
    }
    if (!mounted) return;
    setState(() {
      _authorizedOrNot = authenticated ? true : false;
    });
  }

  @override
  void initState() {
   
    super.initState();
    _authenticateMe();
  }
  @override
  Widget build(BuildContext context) {
 
    final themeNotifier = Provider.of<ThemeNotifier>(context);
    
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (_) => ThemeNotifier(darkTheme),
        ),
        ChangeNotifierProvider(create: (_) => ImageUploadProvider()),
        ChangeNotifierProvider(
          create: (_) => VideoUploadProvider(),
        ),
        ChangeNotifierProvider(create: (_) => UserProvider()),
      ],
      child: MaterialApp(
        title: "App",
        debugShowCheckedModeBanner: false,
        initialRoute: '/',
        routes: {
          '/search_screen': (context) => SearchScreen(),
          '/setting_page': (context) => settingPage(),
        },
        theme: themeNotifier.getTheme(),
        home: _authorizedOrNot==true ?  FutureBuilder(
          future: _authMethods.getCurrentUser(),
          builder: (context, AsyncSnapshot<User> snapshot) {
 
            if (snapshot.hasData) {
              return HomeScreen();
             
            } else {
              return LoginScreen();
            
            }
          },
        )  : ( Container(child: Center(child: CircularProgressIndicator()),)
      ),)
    );
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63957647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档