graphql_flutter 突变查询需要返回语句 - 不确定如何添加它

问题描述

builder 方法上的变更查询需要返回语句,我不确定在何处以及如何将其添加到以下代码中。我相信变异查询应该返回一个通过或失败的操作。

过去几天我一直在努力解决这个问题。我仍然不知道出了什么问题。

我有这个方法,当用户填写一次性代码时,它会在 onCompleted 回调上调用

createuser 变异查询 -

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 指出了 builder 方法中的一个错误,它确切地说 -

主体可能正常完成,导致返回 'null',但返回类型可能是不可为 null 的类型。 尝试在

添加 return 或 throw 语句

请在 graphq_Flutter 方面具有专业知识的人提供帮助。

谢谢。

解决方法

您是否在 graphql 控制台中检查了您的突变查询,因为突变查询需要返回字段或受影响的行,如图像 graphql mutation query 中所示

如果您运行的 null 安全性良好,那么您还应该考虑您的任何字段(uid、fullname、phone 等)都不会重新调整为 null。

如果问题未解决,您能否提供有关 createUser 的更多详细信息