Flutter: amplify_flutter crashes when using amplify_auth_cognito plugin

Issue

TLDR I have 2 issues: the app errors on load and the entire app running on the simulator crashes when I try to use the auth plugin.

I connected to Amplify using amplify init from the terminal. I am following this example: https://pub.dev/packages/amplify_auth_cognito/example

version: 1.0.0+1
amplify_auth_cognito: 0.4.2
amplify_flutter: 0.4.2

The below examples are code blocks from a super simple widget that only renders some text that says ‘Hi’

ISSUE 1:

It works if I start the app without the AmplifyAuthCognito Plugin:

// Example 1 without auth plugin
  void initState() {
    super.initState();
    _configureAmplify();
  }

  Future<void> _configureAmplify() async {
    try {
      await Amplify.configure(amplifyconfig);
      print('Configure success');
    } on AmplifyAlreadyConfiguredException {
      print('Amplify was already configured.');
    }
  }

Configure Success is printed in the debug terminal.

Next I add the Auth plugin and re build the app:

// Example 2 With Auth plugin
  void initState() {
    super.initState();
    _configureAmplify();
  }

  Future<void> _configureAmplify() async {
    try {
      AmplifyAuthCognito authPlugin = AmplifyAuthCognito();
      await Amplify.addPlugins([authPlugin]);
      await Amplify.configure(amplifyconfig);
      print('Configure success');
    } on AmplifyAlreadyConfiguredException {
      print('Amplify was already configured.');
    }
  }

The build throws an exception with this error in the debug console

AmplifyException (AmplifyException(message: Please check your pubspec.yaml if you are depending on an amplify plugin and not using in your app. Underlying error message: Unable to decode configuration, recoverySuggestion: Remove amplify plugins from your pubspec.yaml that you are not using in your app., underlyingException: null))

If I restart the app with the restart button in VSCode, the error goes away. And I get Configure success in the debug console. What am I missing that makes it crash on the first build but not the restart?

ISSUE 2:
I for this I have the same code in example 2 and I have restarted the app to have a clean debug console. I made a button to check for a current user. This is the button click handler:

  void checkSession() async {
    try {
      if (Amplify.isConfigured) {
        print('checkSession');
        final response = await Amplify.Auth.getCurrentUser();
        print('Amplify.Auth.fetchAuthSession $response');
      }
    } on Exception catch (e) {
      print('Amplify.Auth.fetchAuthSession Error $e');
    }
  }

When Amplify.Auth.* is called the entire app crashes in the simulator with this error:

Amplify/AuthCategory.swift:18: Fatal error: Authentication category is not configured. Call Amplify.configure() before using any methods on the category.
Lost connection to device.

I have been stuck on this for a couple of days and would love an ideas 🙂

Solution

Great suggestions SO world 😉

OK so I finally figured it out. I was connecting to an existing Amplify setup. This meant that I had to skip most of the local Amplify cli setup.

I went back to this: https://docs.amplify.aws/lib/auth/getting-started/q/platform/flutter/#prerequisites

I was convinced I was going to jack something up on AWS but did it anyway

amplify add auth

? Do you want to use the default authentication and security configuration?
    `Default configuration`
? How do you want users to be able to sign in?
    `Username`
? Do you want to configure advanced settings?
    `No, I am done.`

amplify push

I am not sure if I broke anything on AWS yet, but the plugin actions are now working.

HUGE fail with Amplify and Flutter for the error messaging here. Neither of those error messages are remotely close to leading to a correct solution. Luckily I went with the tired and true approach of junk everything and start over.

Answered By – Jordan Papaleo

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.