I am post data using graphql mutation. What i want to do is parse response data and use on widget on my flutter app.
How can I get parse response data from graphql mutation here is my code
`
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 `
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.
here is the json result.
`
{
"data": {
"signup": {
"success": true,
"message": "Code sent to your phone successfully!",
}
}
}
`
Convert response json to dart from this site : https://javiercbk.github.io/json_to_dart/
ClassName classObject = ClassName.fromJson(jsonEncode(mutationResult.data));
You can access like this : classObject.data.signup
and so on.