我正在尝试在单击上发送请求,但是当按下按钮时,我的后端只收到一次请求。在客户端中,当打印变量时,它会打印上一个请求。我想在按下按钮时获得尽可能多的请求,比如更新 UI 的数据。
我是否必须清除客户端缓存或其他内容?
const productsGraphQL2 = """
query {
allIngredients{
name
category{
id
}
}
}
""";
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var query = productsGraphQL;
void calcular() {
setState(() {
print("Sending request");
sendRequest().then((value) => print(value.data));
});
}
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () {
calcular();
},
child: Text("Send request"));
}
Future<QueryResult> sendRequest() async {
print("sending request");
var client2 = GraphQLProvider.of(context).value;
var hello = await client2.query(QueryOptions(
document: gql(productsGraphQL2),
));
return hello;
}
}