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.isConfigured returns false on hot restart #1658

Closed
2 of 9 tasks
amizer12 opened this issue Jun 1, 2022 · 21 comments
Closed
2 of 9 tasks

Amplify.isConfigured returns false on hot restart #1658

amizer12 opened this issue Jun 1, 2022 · 21 comments
Assignees
Labels
bug Something is not working; the issue has reproducible steps and has been reproduced datastore Issues related to the DataStore Category

Comments

@amizer12
Copy link

amizer12 commented Jun 1, 2022

Description

Every time i need to hot restart in the app it throws exception:

PlatformException (PlatformException(AmplifyAlreadyConfiguredException, Translating platform exception failed. Please take a look at https://github.com/aws-amplify/amplify-flutter/issues to see if there are any existing issues that match your scenario, and file an issue with the details of the bug if there isn't., {message: AmplifyPlugins.AWSCognitoAuthPlugin cannot be added after Amplify.configure()., recoverySuggestion: Do not add plugins after calling Amplify.configure()., underlyingError: }, null))

Amplify.isConfigured returns false so it is rightly trying to configure it again - but it already IS configured. This results in need to do a full stop and start of the app in order to see changes done in development.

Categories

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

Steps to Reproduce

Add the following into your app:

void initState() {
    super.initState();
    if (!Amplify.isConfigured) {
      _configureAmplify();     
    }


  Future<void> _configureAmplify() async {
    print (Amplify.isConfigured);
    try {
      await Amplify.addPlugin(AmplifyAuthCognito());
      await Amplify.configure(amplifyconfig);
    } on Exception catch (e) {
      print('An error occurred configuring Amplify: $e');
    }
  }

Screenshots

Screenshot 2022-06-01 at 12 10 56

Platforms

  • iOS
  • Android

Android Device/Emulator API Level

API 31

Environment

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.0.1, on macOS 12.4 21F79 darwin-arm, locale
    en-PL)
[✓] Android toolchain - develop for Android devices (Android SDK version
    32.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.67.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

Dependencies

Dart SDK 2.17.1
Flutter SDK 3.0.1
cognito 2.2.3

dependencies:
- amazon_cognito_identity_dart_2 1.0.5 [convert crypto http]
- amplify_auth_cognito 0.5.0 [amplify_auth_cognito_android amplify_auth_cognito_ios amplify_auth_plugin_interface amplify_core collection flutter plugin_platform_interface]
- amplify_flutter 0.5.0 [amplify_analytics_plugin_interface amplify_api_plugin_interface amplify_auth_plugin_interface amplify_core amplify_datastore_plugin_interface amplify_flutter_android amplify_flutter_ios amplify_storage_plugin_interface collection flutter json_annotation meta plugin_platform_interface]
- cupertino_icons 1.0.4
- flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine]
- flutter_launcher_icons 0.9.2 [args image path yaml]
- flutter_secure_storage 5.0.2 [meta flutter flutter_secure_storage_linux flutter_secure_storage_macos flutter_secure_storage_platform_interface flutter_secure_storage_web flutter_secure_storage_windows]
- flutter_spinkit 5.1.0 [flutter]
- font_awesome_flutter 10.1.0 [flutter]
- form_field_validator 1.1.0 [flutter intl]
- http 0.13.4 [async http_parser meta path]
- loader_overlay 2.0.7 [flutter back_button_interceptor]
- mqtt_client 9.6.8 [typed_data event_bus path crypto meta]
- platform_device_id 1.0.1 [flutter platform_device_id_platform_interface platform_device_id_macos platform_device_id_linux platform_device_id_web platform_device_id_windows device_info]
- provider 6.0.3 [collection flutter nested]
- rflutter_alert 2.0.4 [flutter]
- syncfusion_flutter_charts 20.1.56 [flutter intl vector_math syncfusion_flutter_core]
- syncfusion_flutter_gauges 20.1.56 [flutter syncfusion_flutter_core intl]
- syncfusion_flutter_sliders 20.1.56 [flutter intl syncfusion_flutter_core]

transitive dependencies:
- amplify_analytics_plugin_interface 0.5.0 [amplify_core flutter meta]
- amplify_api_plugin_interface 0.5.0 [amplify_core collection flutter json_annotation meta]
- amplify_auth_cognito_android 0.5.0 [flutter]
- amplify_auth_cognito_ios 0.5.0 [amplify_core flutter]
- amplify_auth_plugin_interface 0.5.0 [amplify_core flutter meta]
- amplify_core 0.5.0 [collection date_time_format flutter meta plugin_platform_interface uuid]
- amplify_datastore_plugin_interface 0.5.0 [flutter meta collection amplify_core]
- amplify_flutter_android 0.5.0 [flutter]
- amplify_flutter_ios 0.5.0 [amplify_core flutter]
- amplify_storage_plugin_interface 0.5.0 [flutter meta amplify_core]
- archive 3.3.0 [crypto path]
- args 2.3.1
- async 2.8.2 [collection meta]
- back_button_interceptor 6.0.0 [collection flutter]
- characters 1.2.0
- charcode 1.3.1
- clock 1.1.0
- collection 1.16.0
- convert 3.0.1 [typed_data]
- crypto 3.0.2 [typed_data]
- date_time_format 2.0.1
- device_info 2.0.3 [flutter device_info_platform_interface]
- device_info_platform_interface 2.0.1 [flutter meta plugin_platform_interface]
- event_bus 2.0.0
- flutter_secure_storage_linux 1.1.0 [flutter flutter_secure_storage_platform_interface]
- flutter_secure_storage_macos 1.1.0 [flutter flutter_secure_storage_platform_interface]
- flutter_secure_storage_platform_interface 1.0.0 [flutter plugin_platform_interface]
- flutter_secure_storage_web 1.0.2 [flutter flutter_web_plugins flutter_secure_storage_platform_interface js]
- flutter_secure_storage_windows 1.1.2 [flutter flutter_secure_storage_platform_interface]
- flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta vector_math]
- http_parser 4.0.1 [collection source_span string_scanner typed_data]
- image 3.1.3 [archive meta xml]
- intl 0.17.0 [clock path]
- js 0.6.4
- json_annotation 4.5.0 [meta]
- material_color_utilities 0.1.4
- meta 1.7.0
- nested 1.0.0 [flutter]
- path 1.8.1
- petitparser 4.4.0 [meta]
- platform_device_id_linux 1.0.0 [flutter]
- platform_device_id_macos 1.0.0 [flutter]
- platform_device_id_platform_interface 1.0.0 [flutter plugin_platform_interface]
- platform_device_id_web 1.0.0 [flutter flutter_web_plugins platform_device_id_platform_interface]
- platform_device_id_windows 1.0.0 [flutter]
- plugin_platform_interface 2.1.2 [meta]
- sky_engine 0.0.99
- source_span 1.8.2 [collection path term_glyph]
- string_scanner 1.1.0 [charcode source_span]
- syncfusion_flutter_core 20.1.56 [vector_math flutter]
- term_glyph 1.2.0
- typed_data 1.3.0 [collection]
- uuid 3.0.6 [crypto]
- vector_math 2.1.2
- xml 5.3.1 [collection meta petitparser]
- yaml 3.1.1 [collection source_span string_scanner]

Device

iPhone SE 15.2

OS

iOS 15.2

CLI Version

8.3.1

Additional Context

No response

@amizer12
Copy link
Author

amizer12 commented Jun 1, 2022

This started to happen after updating Amplify-Flutter from 0.4.2 into 5.0 and from 2.8 flutter to 3.0.1

@haverchuck haverchuck added pending-triage This issue is in the backlog of issues to triage amplify/flutter labels Jun 1, 2022
@haverchuck haverchuck added the to-be-reproduced Issues that have not been reproduced yet, but have reproduction steps provided label Jun 1, 2022
@haverchuck haverchuck self-assigned this Jun 2, 2022
@amizer12
Copy link
Author

amizer12 commented Jun 8, 2022

Hi, were you able to reproduce it ?

@Jordan-Nelson
Copy link
Member

Hello @amizer12 - I will take a look at this today and attempt to reproduce it.

@matthewfx
Copy link

I can confirm that it has been happening to me during hot restarts too. Any idea when it can be resolved?

@Jordan-Nelson Jordan-Nelson added core Issues related to the Amplify Core Plugin and removed amplify/flutter labels Aug 9, 2022
@amizer12
Copy link
Author

amizer12 commented Aug 9, 2022

My workaround for this so far is just to configure VS code to not stop at all exceptions but only on the uncaught ones - this a least does not blow up the app on hot reload:
Screenshot 2022-08-09 at 10 32 08

@matthewfx
Copy link

yeah but that is not ideal ;)

@amizer12
Copy link
Author

amizer12 commented Aug 9, 2022

Not a lot of workarounds are :)

@Jordan-Nelson
Copy link
Member

Hey everyone - Sorry for the delay in an update. I was able to confirm the behavior you are experiencing. isConfigured is false after a hot restart.

This is because isConfigured is state that is held in Dart, and all Dart state is lost when performing a hot restart. Amplify-Flutter currently depends on the native Amplify-iOS and Amplify-Android plugins. Native state is not reset during a hot restart, which is why configure throws an exception after a hot restart.

I think there are a couple options for resolving this. Unfortunately neither of them are great options:

  1. Fetch configuration state from native on load. isConfigured would have to be async, which would be a breaking change in the library. This would also likely be a temporary change. We are working on migrating away from the native Amplify-iOS and Amplify-Android plugins, to a full Dart implementation (you can track progress on the Web and/or Desktop support issues). Once this migration is complete, there would be no need for this to be async. Changing it back to sync would require another breaking change. Given this would be a temporary solution that would involve multiple breaking changes, it is unlikely that we pursue this option.
  2. Track the configuration state on the native side, and ignore subsequent calls to Amplify.configure, essentially making it a no-op on the subsequent calls. Amplify.isConfigured would still be false after a hot restart, but calling configure a second time would not throw an exception. Aside from the fact that this doesn't actually resolve the issue of Amplify.isConfigured being false, the main problem with this approach is that it would hide legitimate exceptions that should be thrown when Amplify.configure is called multiple times. Swallowing these legitimate exceptions is probably not an option.

As I mentioned above, this issue exists because Amplify-Flutter depends on the native Amplify-iOS and Amplify-Android plugins, and we are working on migrating away from these native libraries.

As mentioned by @amizer12, unchecking "All Exceptions" will make it so that you do not see the exceptions. This is personally how I typically have my IDE configured (even when working on non-amplify projects) since seeing caught exceptions can be pretty noisy, but I definitely understand that there are times where you want to break on all exceptions.

I will mark this as a bug.

@Jordan-Nelson Jordan-Nelson added bug Something is not working; the issue has reproducible steps and has been reproduced and removed to-be-reproduced Issues that have not been reproduced yet, but have reproduction steps provided pending-triage This issue is in the backlog of issues to triage labels Aug 9, 2022
@Jordan-Nelson Jordan-Nelson changed the title Amplify.isConfigured returns false on hot restart - iOS Amplify.isConfigured returns false on hot restart Aug 9, 2022
@chuks
Copy link

chuks commented Mar 2, 2023

Hello --any updates on this issue? I am still getting this exception on hot restart. Thanks.

@dnys1
Copy link
Contributor

dnys1 commented Apr 17, 2023

This issue has been addressed in v1.0.0 of Amplify Flutter, which is now stable. This release also includes web and desktop support for Auth, API, Analytics, and Storage. You can see the list of new features and bug fixes in the release notes, and see more details on how to migrate in the upgrade guide.

@dnys1 dnys1 closed this as completed Apr 17, 2023
@dnys1 dnys1 added the fixed-in-release-candidate Issues that have been addressed in the current release-candidate branch label Apr 17, 2023
@krosiek
Copy link

krosiek commented May 19, 2023

amplify_auth_cognito: ^1.1.0
amplify_datastore: ^1.1.0-supports-only-mobile
amplify_api: ^1.1.0
amplify_flutter: ^1.1.0
flutter -v 3.10.1

Experiencing the issue, isConfigured returns false.
Exception:

flutter: An error occurred while configuring Amplify: AmplifyAlreadyConfiguredException {
  "message": "Amplify has already been configured and adding plugins after configure is not supported.",
  "recoverySuggestion": "Check if Amplify is already configured using Amplify.isConfigured."
}

Code:

 try {
      if (Amplify.isConfigured) {
        safePrint('Amplify is already configured. Skipping configuration.');
        return some(unit);
      }
      final auth = AmplifyAuthCognito();
      final dataStorePlugin =
          AmplifyDataStore(modelProvider: ModelProvider.instance);
      final api = AmplifyAPI();
      await Amplify.addPlugins([dataStorePlugin, api, auth]);
      await Amplify.configure(amplifyconfig);
    } catch (e) {
      safePrint('An error occurred while configuring Amplify: $e');
      return none();
    }
    return some(unit);

@Jordan-Nelson Jordan-Nelson reopened this May 19, 2023
@Jordan-Nelson Jordan-Nelson removed the core Issues related to the Amplify Core Plugin label May 19, 2023
@Jordan-Nelson Jordan-Nelson added datastore Issues related to the DataStore Category and removed fixed-in-release-candidate Issues that have been addressed in the current release-candidate branch labels May 19, 2023
@Jordan-Nelson
Copy link
Member

I have reopened the issue as I believe this will still be an issue for DataStore. In v1 all plugins have been rewritten in Dart with the exception of DataStore.

@wojciechzahradnikdeviniti

Hi, any updates on this issue? I still have a problem with hot restart.

@harryhieunguyen
Copy link

Me also having this issue. So If catch exception as already configured, I run other tasks related Amplify normally, weird things is catching another error, that AmplifyAnalyticsPinpoint is not configured, although add all them together at the first run.

Future<void> initAmplifyFlutter() async {
    // add plugins
    if (!Amplify.isConfigured) {
      await Amplify.addPlugins([
        AmplifyAPI(), // must have
        AmplifyAuthCognito(),
        AmplifyAnalyticsPinpoint(),
        AmplifyStorageS3(),
        AmplifyDataStore(modelProvider: ModelProvider.instance)
      ]);
      try {
        await Amplify.configure(amplifyconfig).whenComplete(() async {
          if (Amplify.isConfigured) {
            runRootTasks();
          }
        });
      } on AmplifyAlreadyConfiguredException {
        safePrint('Main.dart AmplifyAlreadyConfiguredException');
        runRootTasks();
      }
    }
  }

@fjnoyp
Copy link
Contributor

fjnoyp commented Jul 7, 2023

Hi @harrynguyen2510 so you're getting an AmpliyfyAnalyticsPinpoint is not configured exception when catching AlreadyConfiguredExceptions and performing a hot restart?

@alterhuman
Copy link

Hey, any update on this? Still getting this issue with DataStore.

@harryhieunguyen
Copy link

it is still same issue. @fjnoyp Not only AmpliyfyAnalyticsPinpoint, whole Amplify service return false although it was configured before.

@stevegaunt
Copy link

@fjnoyp this could do with being fixed soon.. sometimes, the completer within the amplify configure does not return in time before the next check configured is set to true, and we get other exceptions other than the AmplifyAlreadyConfiguredException, for examples
pluginError {
"message": "DataStore plugin has already been added, multiple plugins for DataStore category are currently not supported."
}

@fjnoyp
Copy link
Contributor

fjnoyp commented Aug 23, 2023

Hi all this should be fixed by:

#3497

Which has been released in v1.3.1

Please reopen this issue and comment in case you are still encountering this problem.

@fjnoyp fjnoyp closed this as completed Aug 23, 2023
@afnx
Copy link

afnx commented Oct 2, 2023

@fjnoyp @Jordan-Nelson, I'm still having the same error. My DataStore version is 1.4.2.

Exception has occurred.
PlatformException (PlatformException(AmplifyAlreadyConfiguredException, Translating platform exception failed. 
Please take a look at https://github.com/aws-amplify/amplify-flutter/issues to see if there are any existing issues that match 
your scenario, and file an issue with the details of the bug if there isn't., 
{recoverySuggestion: Do not add plugins after calling `Amplify.configure()`., 
message: AmplifyPlugins.AWSDataStorePlugin cannot be added after `Amplify.configure()`., underlyingError: }, null))}

When I mute all exceptions, DataStore is not synced and returns only empty list.

  Future<void> _configureAmplify() async {
    try {
      if (!Amplify.isConfigured) {
        await Amplify.addPlugins([
          AmplifyAuthCognito(),
          AmplifyDataStore(modelProvider: ModelProvider.instance),
          AmplifyAPI(),
        ]);

        await Amplify.configure(amplifyconfig);

      debugPrint('Amplify configured successfully.');
    } else {
        debugPrint('Amplify is already configured.');
      }
    } on AmplifyAlreadyConfiguredException {
      debugPrint('Amplify configuration failed.');
    }
  }

@soheb12
Copy link

soheb12 commented Oct 13, 2023

I am running into this error now and guess what this error has been around for more than a year.

Bugs like these which are not taken off for months by the amplify team, Always makes me question my decision to move from firebase to amplify. We are infact looking to move back to firebase because the team at amplify is being so careless with the management of this service, its just pathetic.

Not done guys :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working; the issue has reproducible steps and has been reproduced datastore Issues related to the DataStore Category
Projects
None yet
Development

No branches or pull requests