Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amplify Exception Error for AddPlugin #2551

Closed
2 of 13 tasks
mrks89 opened this issue Jan 7, 2023 · 4 comments
Closed
2 of 13 tasks

Amplify Exception Error for AddPlugin #2551

mrks89 opened this issue Jan 7, 2023 · 4 comments
Labels
auth Issues related to the Auth Category pending-close-response-required The issue will be closed if details necessary to reproduce the issue are not provided within 7 days. question A question about the Amplify Flutter libraries

Comments

@mrks89
Copy link

mrks89 commented Jan 7, 2023

Description

I am following the AWS tutorial do include the Auth plugin in my flutter app.
AWS tutorial

However, when retrieving the user session I get an Amplify Exception:

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:base_app/main.dart:47:21)
E/flutter (27806): #3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4992:27)
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. (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. (package:flutter/src/widgets/binding.dart:925:7)
E/flutter (27806): #19 Timer._createTimer. (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

may main.dart:

`import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_authenticator/amplify_authenticator.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:flutter/material.dart';

import 'amplifyconfiguration.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);

@OverRide
State createState() => _MyAppState();
}

class _MyAppState extends State {
@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 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");
}
}),
),
);
}
}
`

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Storage

Steps to Reproduce

  1. Run main.dart

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Android Device/Emulator API Level

API 21

Environment

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.9, on Ubuntu 22.04.1 LTS 5.15.0-56-generic,
    locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2021.3)
[✓] VS Code
[✓] Connected device (3 available)
[✓] HTTP Host Availability

• No issues found!

Dependencies

dev --style=compact
Dart SDK 2.18.5
Flutter SDK 3.3.9
base_app 1.0.0+1

dependencies:
- amplify_auth_cognito 0.6.10 [amplify_auth_cognito_android amplify_auth_cognito_ios amplify_core aws_common collection flutter meta plugin_platform_interface]
- amplify_authenticator 0.2.4 [amplify_auth_cognito amplify_core amplify_flutter async aws_common collection flutter flutter_localizations intl stream_transform]
- amplify_flutter 0.6.10 [amplify_core amplify_datastore_plugin_interface amplify_flutter_android amplify_flutter_ios aws_common collection flutter meta plugin_platform_interface]
- cupertino_icons 1.0.5
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]

transitive dependencies:
- amplify_auth_cognito_android 0.6.10 [flutter]
- amplify_auth_cognito_ios 0.6.10 [amplify_core flutter]
- amplify_core 0.6.10 [aws_common collection flutter intl json_annotation meta plugin_platform_interface uuid]
- amplify_datastore_plugin_interface 0.6.10 [amplify_core collection flutter meta]
- amplify_flutter_android 0.6.10 [flutter]
- amplify_flutter_ios 0.6.10 [amplify_core flutter]
- async 2.9.0 [collection meta]
- aws_common 0.1.1 [async collection http meta stream_transform uuid]
- characters 1.2.1
- clock 1.1.1
- collection 1.16.0
- crypto 3.0.2 [typed_data]
- flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math]
- http 0.13.5 [async http_parser meta path]
- http_parser 4.0.2 [collection source_span string_scanner typed_data]
- intl 0.17.0 [clock path]
- json_annotation 4.7.0 [meta]
- material_color_utilities 0.1.5
- meta 1.8.0
- path 1.8.2
- plugin_platform_interface 2.1.3 [meta]
- sky_engine 0.0.99
- source_span 1.9.0 [collection path term_glyph]
- stream_transform 2.1.0
- string_scanner 1.1.1 [source_span]
- term_glyph 1.2.1
- typed_data 1.3.1 [collection]
- uuid 3.0.6 [crypto]
- vector_math 2.1.2

Device

Xiaomi 11 lite

OS

Android

Deployment Method

Amplify CLI

CLI Version

10.5.2

Additional Context

No response

Amplify Config

"UserAgent": "aws-amplify-cli/2.0",
"Version": "1.0",
"auth": {
"plugins": {
"awsCognitoAuthPlugin": {
"UserAgent": "aws-amplify-cli/0.1.0",
"Version": "0.1.0",
"IdentityManager": {
"Default": {}
},
...

@HuiSF
Copy link
Contributor

HuiSF commented Jan 7, 2023

Hi @mrks89 thanks for opening this issue.

Looking at the exception occurred:

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)

This message suggests that you invoked fetchAuthSession API before Amplify.configure(amplifyconfig) completes.

Looking at your example code, you are invoking fetchAuthSession in the FutureBuilder, which is not guaranteed to run after configure completes. You could await the Future return by Amplify.configure() inside isUserSignedIn before invoking fetchAuthSession. Or you could create a state in your widget, say _isAmplifyConfigured, mark it as true when Amplify.configure completes, and you render the widget only when this state becomes true, example here.

@HuiSF HuiSF added question A question about the Amplify Flutter libraries auth Issues related to the Auth Category pending-response Issue is pending response from the issue requestor labels Jan 7, 2023
@Jordan-Nelson
Copy link
Contributor

@mrks89 - Please let us know if you have any follow up questions. Thanks.

@Jordan-Nelson Jordan-Nelson added pending-close-response-required The issue will be closed if details necessary to reproduce the issue are not provided within 7 days. and removed pending-response Issue is pending response from the issue requestor labels Jan 11, 2023
@nabram
Copy link

nabram commented Jan 18, 2023

Hi Jordan - I actually have a very similar issue. I'm following an AWS tutorial, which up until today, after I updated my Android SDK, I haven't had too much of an issue.

Here's my stuff - I'm a beginner at this so I might have messed something up, but it had been working fine until today.

Launching lib\main.dart on SM S901U in debug mode...
Running Gradle task 'assembleDebug'...
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Debug service listening on ws://127.0.0.1:57740/L4njAY89pIo=/ws
Syncing files to device SM S901U...
I/ionsapplication( 7893): Compiler allocated 6075KB to compile void android.view.ViewRootImpl.performTraversals()
I/flutter ( 7893): Amplify plugin was not added
E/flutter ( 7893): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: AmplifyException(message: Amplify plugin AmplifyAuthCognitoMethodChannel was not added successfully., recoverySuggestion: We currently don't have a recovery suggestion for this exception., underlyingException: AmplifyException(message: MissingPluginException(No implementation found for method addPlugin on channel com.amazonaws.amplify/auth_cognito), recoverySuggestion: null, underlyingException: null))
E/flutter ( 7893): #0      MethodChannelAmplify.addPlugin (package:amplify_flutter/src/method_channel_amplify.dart:66:7)
E/flutter ( 7893): <asynchronous suspension>
E/flutter ( 7893): #1      Future.wait.<anonymous closure> (dart:async/future.dart:522:21)
E/flutter ( 7893): <asynchronous suspension>
E/flutter ( 7893): #2      _configureAmplify (package:reaqtionsapplication/main.dart:28:3)
E/flutter ( 7893): <asynchronous suspension>
E/flutter ( 7893): #3      main (package:reaqtionsapplication/main.dart:12:5)
E/flutter ( 7893): <asynchronous suspension>
E/flutter ( 7893): 

Here's my main dart file

import 'package:reaqtionsapplication/reaqtions_app.dart';
import 'package:flutter/material.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'amplifyconfiguration.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  bool isAmplifySuccessfullyConfigured = false;
  try {
    await _configureAmplify();
    isAmplifySuccessfullyConfigured = true;
  } on AmplifyAlreadyConfiguredException {
    debugPrint('Amplify configuration failed.');
  }

  runApp(
    ProviderScope(
      child: ReAQtionsApp(
        isAmplifySuccessfullyConfigured: isAmplifySuccessfullyConfigured,
      ),
    ),
  );
}

Future<void> _configureAmplify() async {
  await Amplify.addPlugins([
    AmplifyAuthCognito(),
  ]);
  await Amplify.configure(amplifyconfig);
}

And here is the app file

import 'package:reaqtionsapplication/features/airquality/ui/aqi_list/aqi_list_page.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:amplify_authenticator/amplify_authenticator.dart';
import 'package:reaqtionsapplication/common/navigation/router/routes.dart';
import 'package:reaqtionsapplication/common/utils/colors.dart' as constants;

class ReAQtionsApp extends StatelessWidget {
  const ReAQtionsApp({
    required this.isAmplifySuccessfullyConfigured,
    Key? key,
  }) : super(key: key);

  final bool isAmplifySuccessfullyConfigured;

  @override
  Widget build(BuildContext context) {
    final router = GoRouter(
      routes: [
        GoRoute(
          path: '/',
          name: AppRoute.home.name,
          builder: (context, state) => isAmplifySuccessfullyConfigured
              ? const AQIListPage()
              : const Scaffold(
            body: Center(
              child: Text(
                'Tried to reconfigure Amplify; '
                    'this can occur when your app restarts on Android.',
              ),
            ),
          ),
        ),
      ],
      errorBuilder: (context, state) => Scaffold(
        body: Center(
          child: Text(state.error.toString()),
        ),
      ),
    );

    return Authenticator(
      child: MaterialApp.router(
        routeInformationParser: router.routeInformationParser,
        routeInformationProvider: router.routeInformationProvider,
        routerDelegate: router.routerDelegate,
        builder: Authenticator.builder(),
        theme: ThemeData(
          primarySwatch: constants.primaryColor,
          backgroundColor: const Color(0xff82CFEA),
        ),
      ),
    );
  }
}

Finally, here are the dependencies:

version: 1.0.0+1

environment:
  sdk: ">=2.18.4 <3.0.0"

dependencies:
  amplify_api: ^0.6.0
  amplify_auth_cognito: ^0.6.0
  amplify_authenticator: ^0.2.2
  amplify_datastore: ^0.6.0
  amplify_flutter: ^0.6.0
  amplify_storage_s3: ^0.6.0
  cached_network_image: ^3.2.1
  cupertino_icons: ^1.0.2
  flutter:
    sdk: flutter
  flutter_hooks: ^0.18.5+1
  flutter_riverpod: ^1.0.4
  go_router: ^4.1.0
  hooks_riverpod: ^1.0.4
  image_picker: ^0.8.5+3
  intl: ^0.17.0

dev_dependencies:
  flutter_lints: ^2.0.1
  flutter_test:
    sdk: flutter



flutter:
  uses-material-design: true
  plugin:
    platforms:
      android:
        package: io.github.edufolly.flutterbluetoothserial
        pluginClass: FlutterBluetoothSerialPlugin

Thanks so much

@Jordan-Nelson
Copy link
Contributor

Hi @nabram - This appears to be a different exception. I don't think it has the same cause as the original issue. I created a new issue for this: #2583.

@mrks89 - I am going to close this out since we have not heard back. Feel free to open a new issue if you have additional questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth Issues related to the Auth Category pending-close-response-required The issue will be closed if details necessary to reproduce the issue are not provided within 7 days. question A question about the Amplify Flutter libraries
Projects
None yet
Development

No branches or pull requests

4 participants