amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

Amplify.isConfigured returns false on hot restart

Open amizer12 opened this issue 3 years ago • 8 comments

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
  • [X] 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

  • [X] 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 avatar Jun 01 '22 10:06 amizer12

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

amizer12 avatar Jun 01 '22 12:06 amizer12

Hi, were you able to reproduce it ?

amizer12 avatar Jun 08 '22 08:06 amizer12

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

Jordan-Nelson avatar Jun 08 '22 12:06 Jordan-Nelson

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

matthewfx avatar Aug 09 '22 01:08 matthewfx

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

amizer12 avatar Aug 09 '22 08:08 amizer12

yeah but that is not ideal ;)

matthewfx avatar Aug 09 '22 09:08 matthewfx

Not a lot of workarounds are :)

amizer12 avatar Aug 09 '22 14:08 amizer12

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 avatar Aug 09 '22 15:08 Jordan-Nelson

I see exceptions thrown that are wrapped by PlatformExeption in this reported issue which indicates it may be related to https://github.com/aws-amplify/amplify-flutter/issues/2238

dorontal avatar Oct 12 '22 15:10 dorontal

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

chuks avatar Mar 02 '23 23:03 chuks

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 avatar Apr 17 '23 19:04 dnys1

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);

krosiek avatar May 19 '23 10:05 krosiek

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.

Jordan-Nelson avatar May 19 '23 11:05 Jordan-Nelson

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

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();
      }
    }
  }

harrynguyen2510 avatar Jun 15 '23 03:06 harrynguyen2510

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

fjnoyp avatar Jul 07 '23 20:07 fjnoyp

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

alterhuman avatar Jul 22 '23 07:07 alterhuman

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

harrynguyen2510 avatar Jul 23 '23 10:07 harrynguyen2510

@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." }

stevegaunt avatar Jul 25 '23 09:07 stevegaunt

Hi all this should be fixed by:

https://github.com/aws-amplify/amplify-flutter/pull/3497

Which has been released in v1.3.1

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

fjnoyp avatar Aug 23 '23 20:08 fjnoyp

@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.');
    }
  }

afnx avatar Oct 02 '23 04:10 afnx

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 :/

soheb12 avatar Oct 13 '23 21:10 soheb12