【问题标题】:graphql_flutter mutation query expects return statement - not sure how to add itgraphql_flutter 突变查询需要返回语句 - 不知道如何添加它
【发布时间】:2023-04-01 09:35:01
【问题描述】:

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

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

我有这个方法,当用户填写一次性代码时,它会在 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”,但返回类型可能是不可为空的类型。 尝试在

处添加return或throw语句

请具有 graphq_flutter 专业知识的人提供帮助。

谢谢。

【问题讨论】:

    标签: flutter dart graphql-flutter


    【解决方案1】:

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

    如果您正在运行可靠的 null 安全性,那么您还应该考虑您的任何字段(uid、fullname、phone...)都没有重新调整 null。

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

    【讨论】:

    • 你好 ruchit,我已经添加了我的变异查询,我的查询不返回任何空值。我也试过graphiql中的查询,它有效。
    猜你喜欢
    • 2017-09-17
    • 2016-12-05
    • 2020-03-09
    • 2017-09-25
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 2015-02-10
    相关资源
    最近更新 更多