Flutter集成Amplify Auth Plugin时“插件未添加”报错排查
Flutter + Amplify 用户认证获取会话失败,提示Auth插件未添加
报错日志
> Launching lib/main.dart on 2109119DG in debug mode... Running Gradle task 'assembleDebug'... ✓ Built build/app/outputs/flutter-apk/app-debug.apk. Debug service listening on ws://xxxx Syncing files to device 2109119DG... I/amplify:flutter:auth_cognito(27806): Added Auth plugin E/flutter (27806): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: AmplifyException(message: Auth plugin has not been added to Amplify, recoverySuggestion: Add Auth plugin to Amplify and call configure before calling Auth related APIs, underlyingException: null) E/flutter (27806): #0 AuthCategory.fetchAuthSession (package:amplify_core/src/category/amplify_auth_category.dart:189:11) E/flutter (27806): #1 _MyAppState.isUserSignedIn (package:base_app/main.dart:37:39) E/flutter (27806): #2 _MyAppState.build (package:flutter/src/widgets/framework.dart:4992:27) E/flutter (27806): #3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4878:15) E/flutter (27806): #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4878:15) E/flutter (27806): #5 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11) E/flutter (27806): #6 Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5) E/flutter (27806): #7 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4859:5) E/flutter (27806): #8 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5041:11) E/flutter (27806): #9 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4853:5) E/flutter (27806): #10 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16) E/flutter (27806): #11 Element.updateChild (package:flutter/src/widgets/framework.dart:3592:18) E/flutter (27806): #12 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1195:16) E/flutter (27806): #13 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1164:5) E/flutter (27806): #14 RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1111:18) E/flutter (27806): #15 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2605:19) E/flutter (27806): #16 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1110:13) E/flutter (27806): #17 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:945:7) E/flutter (27806): #18 WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:925:7) E/flutter (27806): #19 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15) E/flutter (27806): #20 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19) E/flutter (27806): #21 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5) E/flutter (27806): #22 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12) E/flutter (27806): D/AWSMobileClient(27806): Using the SignInProviderConfig from `awsconfiguration.json`. D/AWSMobileClient(27806): Inspecting user state details I/flutter (27806): Successfully configured D/AWSMobileClient(27806): Inspecting user state details I/TetheringManager(27806): registerTetheringEventCallback:com.dontknow.base_app D/AWSMobileClient(27806): hasFederatedToken: false provider: cognito-idp.eu-central-1.amazonaws.com/eu-central-1_xxxx D/AWSMobileClient(27806): hasFederatedToken: false provider: cognito-idp.eu-central-1.amazonaws.com/eu-central-1_xxxx D/AWSMobileClient(27806): Inspecting user state details D/AWSMobileClient(27806): hasFederatedToken: true provider: cognito-idp.eu-central-1.amazonaws.com/eu-central-1_xxxx D/AWSMobileClient(27806): waitForSignIn: userState:SIGNED_IN D/AWSMobileClient(27806): getCredentials: Validated user is signed-in
问题说明
严格按照AWS教程实现,在调用Amplify.configure前已添加Auth插件,日志也显示插件已加载,但获取会话时仍提示插件未添加。
相关代码(main.dart)
State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override void initState() { super.initState(); _configureAmplify(); } void _configureAmplify() async { try { await Amplify.addPlugin(AmplifyAuthCognito()); await Amplify.configure(amplifyconfig); print('Successfully configured'); } on Exception catch (e) { print('Error configuring Amplify: $e'); } } Future<bool> isUserSignedIn() async { final result = await Amplify.Auth.fetchAuthSession(); return result.isSignedIn; } @override Widget build(BuildContext context) { return Authenticator( child: MaterialApp( builder: Authenticator.builder(), home: FutureBuilder( future: isUserSignedIn(), builder: (BuildContext ctx, AsyncSnapshot snapshot) { if (snapshot.data == null) { return const Center( child: CircularProgressIndicator(), ); } else { return Text("logged in"); } }), ), ); } }
解决方案
问题核心是Amplify初始化完成前,FutureBuilder就调用了isUserSignedIn()。initState中的_configureAmplify是异步方法,不会阻塞UI构建,导致build执行时Amplify还未完成插件添加和配置。
修复步骤
- 在状态类中添加标记变量,记录Amplify是否初始化完成:
bool _amplifyConfigured = false;
- 修改
_configureAmplify方法,初始化完成后更新状态:
void _configureAmplify() async { try { await Amplify.addPlugin(AmplifyAuthCognito()); await Amplify.configure(amplifyconfig); setState(() { _amplifyConfigured = true; }); print('Successfully configured'); } on Exception catch (e) { print('Error configuring Amplify: $e'); } }
- 在
build方法中,仅当Amplify初始化完成后才渲染FutureBuilder,否则显示加载状态:
@override Widget build(BuildContext context) { return Authenticator( child: MaterialApp( builder: Authenticator.builder(), home: _amplifyConfigured ? FutureBuilder( future: isUserSignedIn(), builder: (BuildContext ctx, AsyncSnapshot snapshot) { if (snapshot.data == null) { return const Center(child: CircularProgressIndicator()); } else { return Text("logged in"); } }) : const Center(child: CircularProgressIndicator()), ), ); }
额外提示
- 也可以将
isUserSignedIn的调用放在Amplify初始化完成后,比如在_configureAmplify的setState前执行,将登录状态保存到变量,避免FutureBuilder重复调用。 - 确认
amplifyconfig常量正确导入,配置文件内容无误。
内容的提问来源于stack exchange,提问作者Muleque
相关产品推荐
相关产品推荐

