Home > Net >  async/await method throws exception -type int is not a subtype of bool
async/await method throws exception -type int is not a subtype of bool

Time:01-24

I am calling a graphql endpoint which returns success, but I do get an exception on the calling method.

Here is my calling method -

await AmplifyInstance()// this is where I get the exception. Snip below
     .createUserOnAzureCosmosDB(user)
        .then((result) {
            print(result['data']['userPhoneNumber']);
              _intlPhoneFieldController.text =
                    (result['data']['userPhoneNumber'].toString())
                                .substring(1);
                     _incrementStep('continueOnProfilePictureWidget');
});

Here is the called method -

Future<dynamic> createUserOnAzureCosmosDB(User user) async {
    HttpLink link = GlobalVariables().graphqlEndpoint;
    GraphQLClient graphQlClient = GraphQLClient(
      link: link,
      cache: GraphQLCache(
        store: InMemoryStore(),
      ),
    );
    try {
      QueryResult mutationResult = await graphQlClient.mutate(
        //Mutation query here
      if (mutationResult.data?['createUser'] != null) {
        print('Created user on Cosmos DB');
        registerUserStatus['result'] = true;
        registerUserStatus['data'] = mutationResult.data?['createUser'];
      }
    } on ApiException catch (e) {
      print('Mutation failed: $e');
      registerUserStatus['result'] = false;
      registerUserStatus['errorMessage'] = e.message;
    }

    return registerUserStatus;
  }

And the returned registerUserStatus is just an array -

var registerUserStatus = {};

Here is the exception - enter image description here

UPDATE eamirho3ein Here is the result of print("result=$result);

I/flutter (14224): result = {result: true, data: {__typename: User, partitionKey: user, userPhoneNumber: 14160000000, userDisplayName: testuser, avatarUrl: www.url.com, createdAt: Today}}

CodePudding user response:

This is not actually an answer, but rather a way to find the answer more easily yourself:

then chains make it increasingly hard to find your problem, because the compiler/debugger/IDE has a harder time pointing you to it. So don't do it.

With async/await available from the beginning, there never has been a reason to use then in any Dart program.

await AmplifyInstance().createUserOnAzureCosmosDB(user).then((result) { 

Is equivalent to just writing:

final result = await AmplifyInstance().createUserOnAzureCosmosDB(user);

And then continuing on with the code you had put in the lambda function in the then part. Obviously, you need to remove the closing bracket somewhere too now.

This way, your error will actually pop up where it happens, not at the await of a huge chain that leaves you wondering what the problem might be.

  • Related