简体   繁体   中英

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.

did you check your mutation query in graphql console because mutation query require field returning or affected_rows as show in imagegraphql变异查询

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM