2

我正在使用 graphql_flutter 3.1.0 并已开始对我的应用程序进行身份验证,并且需要在成功创建时将用户设置为我的 Provider 状态。我对 CreateUser 的突变目前是:

final String createUserMutation = """
mutation CreateUser(\$firstName: String!, \$username: String!, \$phoneNumber: String!) {
  CreateUser(firstName: \$firstName, username: \$username, phoneNumber: \$phoneNumber, role: USER) {
    id,
    firstName,
    username,
    phoneNumber,
    role
  }
}
""";

这成功地在我的 Neo4j 数据库中创建了用户。如果我在我的 GraphQL 界面中运行相同的查询,我会得到完全符合预期的响应:

{
  "data": {
    "CreateUser": {
      "id": "e2a44525-fd46-4aae-8e00-38d263b769e6",
      "firstName": "Sam",
      "username": "sgamgee",
      "phoneNumber": "3216543216",
      "role": "USER"
    }
  }
}

在我的SignupForm小部件中,我将表单包装起来Mutation并将其传递createUserMutationMutationOptions类似的东西:

class SignupFormState extends State<SignupForm> {
  final _formKey = GlobalKey<FormState>();

  final user = User();

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(35),
      child: Mutation(
        options: MutationOptions(
          documentNode: gql(createUserMutation)
        ),
        builder: (RunMutation runMutation, QueryResult result) {
          return UserCreateForm(
            formKey: _formKey, 
            user: user,
            runMutation: runMutation,
            result: result
          );
        },
      )
    );
  }
}

到目前为止一切都很好,没有错误。当我尝试通过 onPressed 闭包提交表单时:

onPressed: () async {
  final form = _formKey.currentState;
  if (form.validate()) {
    form.save();
                    
    runMutation({
      'firstName': user.firstName,
      'username': user.username,
      'phoneNumber': user.phoneNumber
    });
                    
    if (result.hasException) {
      return AlertBox(
        type: AlertType.error,
        text: result.exception.toString()
      );
    } else if (result.loading) {
      return const Center(
        child: CircularProgressIndicator(),
      );
    } else {
      // NOTE: result.data is null though the mutation is running successfully. 
      var createdUser = result.data['CreateUser'];
      Provider.of<UserProvider>(context, listen: false).signUp(createdUser);
                      
      if (Provider.of<UserProvider>(context, listen: false).isAuthenticated()) {
        Navigator.pushReplacementNamed(context, '/signup-success');
      } else {
        Navigator.pushReplacementNamed(context, '/signup');
      }
    }
  }
}

result.data 始终为空。我既不知道也不知道runMutation未来result,因此不能等待。也存在并且视情况而定并且是虚假或真实的hasExceptionloading有没有人注意到可能导致result.data始终为空的其他任何事情?

4

0 回答 0