jamiechoo

 

Flutter app fails to configure AWS Amplify (Null check operator used on a null value)

Describe the bug
I ran into a problem while upgrading a production app from AWS Amplify Flutter 0.1.X to ^0.2.0. The Flutter app fails to configure the Amplify packages without error. I then created a brand new Flutter application, following the steps on the Getting Started documentation and the Authentication page. I experienced the exact same error, despite building a project from scratch, including the AWS resources.

To Reproduce
Steps to reproduce the behavior:

  1. Follow the steps on the Getting Started documentation.
  2. Add the auth plugin following these steps.
  3. Add a test file module, such as this one:
import 'package:flutter_test/flutter_test.dart';
    import 'package:amplify_flutter/amplify.dart';
    import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
    import 'package:aws_amplify_flutter_app/amplifyconfiguration.dart';
    
    void main() {
      test('can configure Amplify', () async {
        AmplifyAuthCognito authPlugin = AmplifyAuthCognito();
        await Amplify.addPlugin(authPlugin);
    
        try {
          await Amplify.configure(amplifyconfig);
        } on AmplifyAlreadyConfiguredException {
          print("Tried to reconfigure Amplify; this can occur when your app restarts on Android.");
        }
      });
    }
 
  1. Run the test using a command like: flutter test test\my_test.dart

Actual Behavior
When I run the test file above, I get the following output:

F:\dev\personal\aws_amplify_flutter_app>flutter test test\widget_test.dart
00:36 +0: can configure Amplify
Amplify plugin was not added
00:36 +0 -1: can configure Amplify [E]
  AmplifyException(message: Amplify plugin AmplifyAuthCognito was not added successfully., recoverySuggestion: We currently don't have a recovery suggestion for this exception., underlyingException: Null check operator used on a null value)
  package:amplify_flutter/amplify.dart 101:9  AmplifyClass.addPlugin

00:36 +0 -1: Some tests failed.
 

I had a suspicion that the Amplify packages don't play nicely with the test runner, so I ran the built the project and ran the tests on my Samsung Galaxy S21 5G running Android 11. When I ran flutter run test\my_test.dart, I observed this output:

F:\dev\personal\aws_amplify_flutter_app>flutter run test\widget_test.dart
Running "flutter pub get" in aws_amplify_flutter_app...          1,920ms
Launching test\widget_test.dart on SM G991U in debug mode...
Running Gradle task 'assembleDebug'...                            301.8s
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...                14.1s
I/flutter (14091): 00:00 +0: can configure Amplify
I/flutter (14091): Amplify plugin was not added
I/flutter (14091): 00:00 +0: can configure Amplify [E]
I/flutter (14091):   AmplifyException(message: Amplify plugin AmplifyAuthCognito was not added successfully., recoverySuggestion: We currently don't have a recovery suggestion for this exception., underlyingException: Null check operator used on a null value)
I/flutter (14091):   package:amplify_flutter/amplify.dart 101:9                         AmplifyClass.addPlugin
I/flutter (14091):   ===== asynchronous gap ===========================
I/flutter (14091):   F:/dev/personal/aws_amplify_flutter_app/test/widget_test.dart 9:5  main.<fn>
I/flutter (14091):   ===== asynchronous gap ===========================
I/flutter (14091):   package:test_api/src/backend/declarer.dart 199:9                   Declarer.test.<fn>.<fn>
I/flutter (14091):   ===== asynchronous gap ===========================
I/flutter (14091):   package:test_api/src/backend/declarer.dart 197:7                   Declarer.test.<fn>
I/flutter (14091):   ===== asynchronous gap ===========================
I/flutter (14091):   package:test_api/src/backend/invoker.dart 257:7                    Invoker._waitForOutstandingCallbacks.<fn>
I/flutter (14091):
I/flutter (14091): Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
I/flutter (14091): For example, 'dart test --chain-stack-traces'.
I/flutter (14091): 00:00 +0 -1: Some tests failed.

(Some UI debug messages elided...)

Syncing files to device SM G991U...                                989ms

Flutter run key commands.
r Hot reload.
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

 Running with sound null safety 

An Observatory debugger and profiler on SM G991U is available at: http://127.0.0.1:62306/ZudipYxxYjM=/

Application finished.
 

In other words, I saw the same results running in the test runner as on a real device.

Expected behavior
I expect that the test will pass without issue as the Amplify CLI should have properly created an amplifyconfiguration.dart file and the packages load the configuration successfully.

Screenshots
Here's a screenshot of debugging the await Amplify.configure(amplifyconfig); line in Android Studio:
debugging

Note that in the screenshot I'm hovering over line 35 of method_channel_auth_cognito.dart where the watch window shows the same message ("Null check operator used on a null value") as can be seen from the test output.

Platform
Amplify Flutter current supports iOS and Android. This issue is reproducible in (check all that apply):
[X] Android
[X] iOS

Output of flutter doctor -v
 
Dependencies (pubspec.lock)
 

Smartphone (please complete the following information):

  • Device: Tested on both Samsung Galaxy S21 and iPhone
  • OS: Android or iOS
  • Browser: Not tested
  • Version: Android 11

Additional context
pubspec.yaml

name: aws_amplify_flutter_app
description: A new Flutter application.
publish_to: 'none'
version: 1.0.0+1

environment:
  sdk: ">=2.15.1 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # Version 0.2.10 of all AWS Amplify packages are installed
  amplify_flutter: ^0.2.0
  amplify_auth_cognito: ^0.2.0
  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^1.0.0

flutter:
  uses-material-design: true
 

amplifyconfiguration.dart (obfuscated)

const amplifyconfig = ''' {
    "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": {}
                },
                "CredentialsProvider": {
                    "CognitoIdentity": {
                        "Default": {
                            "PoolId": "us-east-1:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
                            "Region": "us-east-1"
                        }
                    }
                },
                "CognitoUserPool": {
                    "Default": {
                        "PoolId": "us-east-1_abcdefghi",
                        "AppClientId": "abcdefghijklmnopqrstuvwxy",
                        "Region": "us-east-1"
                    }
                },
                "Auth": {
                    "Default": {
                        "authenticationFlowType": "USER_SRP_AUTH",
                        "socialProviders": [],
                        "usernameAttributes": [],
                        "signupAttributes": [
                            "EMAIL"
                        ],
                        "passwordProtectionSettings": {
                            "passwordPolicyMinLength": 8,
                            "passwordPolicyCharacters": []
                        },
                        "mfaConfiguration": "OFF",
                        "mfaTypes": [
                            "SMS"
                        ],
                        "verificationMechanisms": [
                            "EMAIL"
                        ]
                    }
                }
            }
        }
    }
}''';

posted on 2024-07-23 21:12  jamiechoo  阅读(40)  评论(0)    收藏  举报

导航