flutterdartgraphql-flutter

graphql_flutter mutation query expects return statement - not sure how to add it


The mutation query on the builder method is expecting a return statement, which I am not sure where and how to add to the below code. I believe the mutation query should return a pass or fail for the operation.

I have been trying to figure this out for the past few days. I still haven't got a clue on what is wrong.

I have this method which is called on the onCompleted callback when the user fills in the one time code.

createUser Mutation Query -

class GraphQlMutations {
  final String createUser = r'''
  mutation createUser($uid: ID!, $fullname: String!, $phone: String!, $photourl: String, $createdtime: String){
    createUser(uid: $uid, fullname: $fullname, phone: $phone, photourl: $photourl, createdtime: $createdtime){
      uid
      fullname
      phone
      photourl
      createdtime
    }
  }
''';
}

void signInWithPhoneAuthCredential(
      PhoneAuthCredential phoneAuthCredential) async {
    setState(() {
      showLoading = true;
    });
    try {
      setState(() {
        showLoading = false;
      });
      final authCredential =
          await _auth.signInWithCredential(phoneAuthCredential);
      User? user = authCredential.user;
      print(user);
      if (authCredential.user != null) {
        Mutation(
          options: MutationOptions(
              document: gql(GraphQlMutations().createUser),
              onCompleted: (dynamic resultData) {
                ScaffoldMessenger.of(context)
                    .showSnackBar(SnackBar(content: Text('New user added')));
              }),
          builder: (RunMutation createUser, QueryResult? result) {
            if (user != null) {
              final DateTime? createdTime = user.metadata.creationTime;
              final DateFormat formatter = DateFormat('yyyy-MM-dd');
              final String userCreatedTime = formatter.format(createdTime!);
              try {
                createUser({
                  'uid': user.uid,
                  'fullname': _fullname.text,
                  'phone': user.phoneNumber,
                  'photourl': 'https://www.bol.com',
                  'createdtime': userCreatedTime,
                });
                Navigator.pushReplacement(context,
                    MaterialPageRoute(builder: (context) => Conversations()));
              } catch (e) {}
            }
          },
        );
      }
    } 
  }

Vscode points out an error on the builder method which exactly says -

The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type. Try adding either a return or a throw statement at the

Somebody with expertise in graphq_flutter please chime in to help.

Thanks.


Solution

  • did you check your mutation query in graphql console because mutation query require field returning or affected_rows as show in image graphql mutation query

    and if you are running sound null safety then you should also consider that any of your fields(uid, fullname, phone,...) aren't retuning null.

    and can you provide more detail on createUser if problem not resolve