我已经成功实现了用户身份验证,一切正常,我只想高效地管理currentUser
。
我想登录一次并想随时访问应用中的currentUser
。
FirebaseAuth.instance.currentUser()
是Future<FirebaseUser>
,我想立即获得,我无法弄清楚如何管理它,我认为不需要为此添加代码,我只是在寻找一个想法。
答案 0 :(得分:1)
正如塞巴斯蒂安所说,您应该研究Provider或任何其他状态管理工具/模式,但是对于我要使用的示例,您将需要Provider程序包。
Firebase Auth有一个λ cmake --build .
Microsoft (R) Build Engine version 15.9.21+g9802d43bc3 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Checking Build System
CMake does not need to re-run because C:/Users/mulderpa/cppsandbox/sdb2/build/CMakeFiles/generate.stamp is up-to-date.
Building Custom Rule C:/Users/mulderpa/cppsandbox/sdb2/CMakeLists.txt
CMake does not need to re-run because C:/Users/mulderpa/cppsandbox/sdb2/build/CMakeFiles/generate.stamp is up-to-date.
main.cc
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(18): error C2228: left of '.begin' must have class/struct/union [C:\Users\mulderpa\cppsandbox\sdb2\build\main.v cxproj]
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(18): error C2672: 'std::replace': no matching overloaded function found [C:\Users\mulderpa\cppsandbox\sdb2\buil d\main.vcxproj]
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(18): error C2780: 'void std::replace(const _FwdIt,const _FwdIt,const _Ty &,const _Ty &)': expects 4 arguments - 3 provided [C:\Users\mulderpa\cppsandbox\sdb2\build\main.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\algorithm(1607): note: see declaration of 'std::repl
ace'
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(19): error C2672: 'std::replace': no matching overloaded function found [C:\Users\mulderpa\cppsandbox\sdb2\buil d\main.vcxproj]
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(19): error C2780: 'void std::replace(const _FwdIt,const _FwdIt,const _Ty &,const _Ty &)': expects 4 arguments - 3 provided [C:\Users\mulderpa\cppsandbox\sdb2\build\main.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\algorithm(1607): note: see declaration of 'std::repl
ace'
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(21): error C2440: 'initializing': cannot convert from 'std::vector<char,std::allocator<char>> (__cdecl *)(std:: istreambuf_iterator<char,std::char_traits<char>>,std::istreambuf_iterator<char,std::char_traits<char>> (__cdecl *)(void))' to 'std::vector<char,std::allo cator<char>>' [C:\Users\mulderpa\cppsandbox\sdb2\build\main.vcxproj]
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(21): note: No constructor could take the source type, or constructor overload resolution was ambiguous
流,如果用户已登录,则发出当前的onAuthStateChanged
;如果未登录,则发出FirebaseUser
。因此,您可以做的是包装{{1} }和null
,并且在应用的任何屏幕中,您都可以访问用户信息
示例:
MaterialApp
值得注意的是StreamProvider
是流式的,因此,如果注销或登录事件发生,它将根据该事件立即发出数据。
答案 1 :(得分:0)
您应该检查Provider软件包