https://dev.to/movingmelody/flutter-firebase-google-sign-in-auth-with-splashscreen-using-updated-api-23ad
我嘗試在此站點的初始螢屏期間使用 google auth 登錄代碼,但我仍然收到遲到的錯誤
class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}
class _SplashScreenState extends State<SplashScreen> {
final FirebaseAuth _auth = FirebaseAuth.instance;
late User _user;
@override
void initState() {
super.initState();
initializeUser();
_initUser();
}
Future initializeUser() async {
await Firebase.initializeApp();
final User? firebaseUser = await FirebaseAuth.instance.currentUser;
await firebaseUser!.reload();
_user = (await _auth.currentUser)!;
// get User authentication status here
}
_initUser() async {
if (_auth.currentUser != null) {
Timer(
Duration(seconds: 2),
() => Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) =>
MainScreen(_user)),
(Route<dynamic> route) => false),
);
} else {
Timer(Duration(seconds: 1),
() => Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) =>
MyHomePage()),
(Route<dynamic> route) => false),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text("Splash Screen"),
),
);
}
}
MyHomePage() 或 MainScreen(_user) 的內容也按原樣鍵入了站點代碼,但錯誤仍然存??在。在gradle升級之前,這段代碼運行正常,但是在jdk版本up和gradle升級之后,該死的遲到的錯誤并沒有停止。
這是錯誤
Performing hot restart...
Syncing files to device sdk gphone x86...
Restarted application in 1,320ms.
W/DynamiteModule( 6646): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule( 6646): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller( 6646): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
I/om.gwon.goodma( 6646): The ClassLoaderContext is a special shared library.
I/om.gwon.goodma( 6646): The ClassLoaderContext is a special shared library.
I/TetheringManager( 6646): registerTetheringEventCallback:com.gwon.goodman
V/NativeCrypto( 6646): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 294 native methods...
W/om.gwon.goodma( 6646): Accessing hidden method Ljava/security/spec/ECParameterSpec;->getCurveName()Ljava/lang/String; (greylist, reflection, allowed)
I/ProviderInstaller( 6646): Installed default security provider GmsCore_OpenSSL
I/FirebaseAuth( 6646): [FirebaseAuth:] Preparing to create service connection to fallback implementation
W/System ( 6646): Ignoring header X-Firebase-Locale because its value was null.
W/om.gwon.goodma( 6646): Accessing hidden field Ljava/net/Socket;->impl:Ljava/net/SocketImpl; (greylist, reflection, allowed)
W/om.gwon.goodma( 6646): Accessing hidden method Ldalvik/system/CloseGuard;->get()Ldalvik/system/CloseGuard; (greylist,core-platform-api, linking, allowed)
W/om.gwon.goodma( 6646): Accessing hidden method Ldalvik/system/CloseGuard;->open(Ljava/lang/String;)V (greylist,core-platform-api, linking, allowed)
W/om.gwon.goodma( 6646): Accessing hidden method Ljava/security/spec/ECParameterSpec;->setCurveName(Ljava/lang/String;)V (greylist, reflection, allowed)
W/om.gwon.goodma( 6646): Accessing hidden method Ldalvik/system/BlockGuard;->getThreadPolicy()Ldalvik/system/BlockGuard$Policy; (greylist,core-platform-api, linking, allowed)
W/om.gwon.goodma( 6646): Accessing hidden method Ldalvik/system/BlockGuard$Policy;->onNetwork()V (greylist, linking, allowed)
======== Exception caught by widgets library =======================================================
The following LateError was thrown building MainScreen(dirty, dependencies: [_InheritedProviderScope<Pro?>], state: _MainScreenState#c8d34):
LateInitializationError: Field '_instance@21075166' has not been initialized.
The relevant error-causing widget was:
MainScreen MainScreen:file:///F:/flutter project/good_man/lib/main.dart:81:25
該站點上的代碼是 2020 年 11 月的代碼,但與此同時,Timer 和 google auth 的規則發生了變化......即使您撰寫使用 Future 并檢查 null 的相同代碼,該錯誤仍然存??在使用 ![在此處輸入影像描述][1]
這不是最后一個遲到的問題帖子的副本。在看到那篇文章的建議后,我寫下了我第一次寫這段代碼的博客的相同代碼,但我仍然為保持Late錯誤而感到尷尬。
uj5u.com熱心網友回復:
即使您在 initUser() 中初始化這些變數,但如果您在 build() 方法中使用變數,您將收到此錯誤,因為 initUser() 是異步的,這意味著從集合中獲取資料需要時間。要解決此問題,您可以執行以下操作:
@override
void initState() {
super.initState();
_initUser().whenComplete((){
setState(() {});
});
}
這將使用新值重建小部件樹。
uj5u.com熱心網友回復:
您不需要使用Late,您應該使用空檢查運算子'?'
只需更換
Late User _user
和
User? _user
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/420866.html
標籤:
下一篇:僅在顫動中淡入影片
