简体   繁体   English

如何使用 flutter_graphql 插件解析 flutter 中的 graphql 响应

[英]How to parse a graphql response in flutter using flutter_graphql plugin

I am post data using graphql mutation.我正在使用 graphql 突变发布数据。 What i want to do is parse response data and use on widget on my flutter app.我想要做的是解析响应数据并在我的 flutter 应用程序的小部件上使用。

How can I get parse response data from graphql mutation here is my code我如何从 graphql 突变中获取解析响应数据这是我的代码

` `

class RegisterService {
  final String name, phone, email, password;
  final VoidCallback onCompleted, onError;
  RegisterService({
    this.name = "",
    this.phone = "",
    this.email = "",
    this.password = "",
    required this.onCompleted,
    required this.onError,
  });

  Future<Object?> sendData() async {
    String addData = """
mutation signup(\$signupInput: SignupInput!) {
  signup(signupInput: \$signupInput) {
    success
    message
    phone
  }
}
""";
    final variable = {
      "signupInput": {
        "name": name,
        "phone": phone,
        "email": email,
        "password": password,
      }
    };
    debugPrint("SendingData");

    QueryResult mutationResult = await qlclient.mutate(MutationOptions(
      document: gql(addData),
      variables: variable,
      onCompleted: (_) {
        onCompleted();
      },
      onError: (_) => onError(),
    ));


    debugPrint("${mutationResult.data?["signup"]["message"]}");
    return mutationResult;
  }
}

` `

here is function `这是 function`

  void buttonPressed() async {
    changeStatus(true);
    debugPrint("$_isLoading");
    Timer(Duration(seconds: 3), () {
      if (formKey.currentState!.validate()) {
        debugPrint("Successful");
      } else {
        autoValidate = autovalidateModeOnUser;
      }
      var utils =  RegisterService(
          onError: () {},
          onCompleted: (() {
            changeStatus(false);
          }),
          name: nameTextEditingController.text.trim(),
          phone: ("09" + "${phoneTextEditingController.text.trim()}"),
          password: phoneTextEditingController.text.trim(),
          email: emailTextEditingController.text.trim(),
          );
      var res = utils.sendData();
  
      debugPrint("${res}");
      globalKeyScaffoldMessengerState.currentState
          ?.showSnackBar(mySnackBar("utils.sendData()."));
    });

    notifyListeners();
  }

` `

I want to get the response and use it on my widget.我想获得响应并在我的小部件上使用它。 I am using flutter_graphql plugin.我正在使用 flutter_graphql 插件。

here is the json result.这是 json 结果。

` `

{
  "data": {
    "signup": {
      "success": true,
      "message": "Code sent to your phone successfully!",
    }
  }
}

` `

  1. Convert response json to dart from this site: https://javiercbk.github.io/json_to_dart/从该站点将响应 json 转换为 dart: https://javiercbk.github.io/json_to_dart/

  2. ClassName classObject = ClassName.fromJson(jsonEncode(mutationResult.data)); ClassName classObject = ClassName.fromJson(jsonEncode(mutationResult.data));

  3. You can access like this: classObject.data.signup and so on.您可以这样访问: classObject.data.signup等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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