I want to send parameters to a GraphQL query from my flutter application, but it doesn’t work. I declared a variable and write to it when I select by which criteria I am going to sort the query.
Query in Flutter:
<code>String getAllPlanner = '''
query{
allPlanners(orderby: $orderby){
code,
error,
list{
id,
service{
id,
}
client{
id,
}
startDate,
endDate,
status
}
}
}
''';
</code>
<code>String getAllPlanner = '''
query{
allPlanners(orderby: $orderby){
code,
error,
list{
id,
service{
id,
}
client{
id,
}
startDate,
endDate,
status
}
}
}
''';
</code>
String getAllPlanner = '''
query{
allPlanners(orderby: $orderby){
code,
error,
list{
id,
service{
id,
}
client{
id,
}
startDate,
endDate,
status
}
}
}
''';
Calling the query:
<code>Query(
options: QueryOptions(document: gql(getAllPlanner)),
builder: (result, {fetchMore, refetch}) {
if (result.isLoading) {
return Container();
}
print(getAllPlanner.toString());
print("Result getAllPlanner = " + result.data.toString());
}
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: listplanner.length,
itemBuilder: (BuildContext context, int index) {
Planner planner = extractRepositoryDataPlanner(
"",
listplanner[index],
);
return PlannerItem(
planner: planner,
index: index,
);
});
} else {
return Container();
}
});
</code>
<code>Query(
options: QueryOptions(document: gql(getAllPlanner)),
builder: (result, {fetchMore, refetch}) {
if (result.isLoading) {
return Container();
}
print(getAllPlanner.toString());
print("Result getAllPlanner = " + result.data.toString());
}
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: listplanner.length,
itemBuilder: (BuildContext context, int index) {
Planner planner = extractRepositoryDataPlanner(
"",
listplanner[index],
);
return PlannerItem(
planner: planner,
index: index,
);
});
} else {
return Container();
}
});
</code>
Query(
options: QueryOptions(document: gql(getAllPlanner)),
builder: (result, {fetchMore, refetch}) {
if (result.isLoading) {
return Container();
}
print(getAllPlanner.toString());
print("Result getAllPlanner = " + result.data.toString());
}
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: listplanner.length,
itemBuilder: (BuildContext context, int index) {
Planner planner = extractRepositoryDataPlanner(
"",
listplanner[index],
);
return PlannerItem(
planner: planner,
index: index,
);
});
} else {
return Container();
}
});
Seleccting criteria and saving in to a varible “orderby”:
<code> RadioListTile(
title: Text(AppLocalizations.of(context)!.dateUpward),
value: sortingOptions.dateUpward,
groupValue: selectedSortingOptions,
onChanged: (value) => setState(() {
selectedSortingOptions = sortingOptions.dateUpward;
orderby = "startDate";
Navigator.pop(context);
}),
),
RadioListTile(
title: Text(AppLocalizations.of(context)!.dateFalling),
value: sortingOptions.dateFalling,
groupValue: selectedSortingOptions,
onChanged: (value){
orderby = "-startDate";
print(orderby);
selectedSortingOptions = sortingOptions.dateFalling;
Navigator.pop(context);
setState(() {});
},
),
</code>
<code> RadioListTile(
title: Text(AppLocalizations.of(context)!.dateUpward),
value: sortingOptions.dateUpward,
groupValue: selectedSortingOptions,
onChanged: (value) => setState(() {
selectedSortingOptions = sortingOptions.dateUpward;
orderby = "startDate";
Navigator.pop(context);
}),
),
RadioListTile(
title: Text(AppLocalizations.of(context)!.dateFalling),
value: sortingOptions.dateFalling,
groupValue: selectedSortingOptions,
onChanged: (value){
orderby = "-startDate";
print(orderby);
selectedSortingOptions = sortingOptions.dateFalling;
Navigator.pop(context);
setState(() {});
},
),
</code>
RadioListTile(
title: Text(AppLocalizations.of(context)!.dateUpward),
value: sortingOptions.dateUpward,
groupValue: selectedSortingOptions,
onChanged: (value) => setState(() {
selectedSortingOptions = sortingOptions.dateUpward;
orderby = "startDate";
Navigator.pop(context);
}),
),
RadioListTile(
title: Text(AppLocalizations.of(context)!.dateFalling),
value: sortingOptions.dateFalling,
groupValue: selectedSortingOptions,
onChanged: (value){
orderby = "-startDate";
print(orderby);
selectedSortingOptions = sortingOptions.dateFalling;
Navigator.pop(context);
setState(() {});
},
),
Testing query: