|
| 1 | +--- |
| 2 | +title: Queries |
| 3 | +sidebar_position: 3 |
| 4 | +--- |
| 5 | + |
| 6 | +### QueryBuilder |
| 7 | + |
| 8 | +The defined logic in [QueryJob](/docs/basics/QueryJob) is bind to the Flutter UI using the `QueryBuilder` Widget. It's basically a `Builder` that takes a `QueryJob` through the `job` named parameter & creates/retrieves the appropriate `Query` and passes it down to the `builder` method |
| 9 | + |
| 10 | +```dart |
| 11 | +class Example extends StatelessWidget { |
| 12 | + const Example({Key? key}) : super(key: key); |
| 13 | +
|
| 14 | + @override |
| 15 | + Widget build(BuildContext context) { |
| 16 | + return QueryBuilder<String, void>( |
| 17 | + job: job, |
| 18 | + externalData: null, |
| 19 | + builder: (context, query) { |
| 20 | + if (!query.hasData) { |
| 21 | + return const CircularProgressIndicator(); |
| 22 | + } |
| 23 | + return Text(query.data!); |
| 24 | + }, |
| 25 | + ); |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +> Here `job` is the same `QueryJob` defined at the first snippet in the [Query Job](/docs/basics/QueryJob) tutorial |
| 31 | +
|
| 32 | +The `externalData` parameter of the `QueryBuilder` is passed to the `task` function of the `QueryJob`. It was discussed previously in [Query Job#External Data](/docs/basics/QueryJob#external-data) section |
| 33 | + |
| 34 | +### Query |
| 35 | +The passed query from the `builder` callback is the appropriate `Query` created based on the logic & configuration defined in the passed `QueryJob` |
| 36 | + |
| 37 | +The `query` parameter aka `Query` contains all the useful getters, properties & methods for rendering data from the query. It contains the state of the current query, the data, the error, the loading status etc along with useful methods such as `refetch` and `setQueryData` |
| 38 | + |
| 39 | +But more importantly, it contains the status of the current `Query`. It has to types of status one is Query Progression status & another is data availability status |
| 40 | + |
| 41 | +You can access them as follows: |
| 42 | +- Progressive status of Query |
| 43 | + - `isSuccess`: When the task function returned data successfully |
| 44 | + - `isError`: When the task function returned an error |
| 45 | + - `isLoading`: When the task function is running |
| 46 | + - `isRefetching`: When new data is being fetched or simply the `refetch` method is executing |
| 47 | + - `isIdle`: When there's no data & `Query`'s task has not been yet run |
| 48 | +- Data availability status of Query |
| 49 | + - `hasData`: When query contains data (expired or not) |
| 50 | + - `hasError`: When the query contains error |
| 51 | + |
| 52 | + |
| 53 | +Now the most important part of query: Data and Error. You can access the data returned from the task using `query.data` or the error `query.error`. Both the data can be null. So always check if the data/error is null before accessing it |
| 54 | + |
| 55 | +:::info |
| 56 | +Don't use only `query.isLoading` to check if the data is available or not as the query can be failed & at this time `data` which can cause UI Exceptions. So use `query.hasData` always to check if `data` is available yet or not or use both together |
| 57 | +::: |
| 58 | + |
| 59 | +Another important part of this is `refetch`. Well, you can use it to manually trigger refetch or want the query to get newest data |
| 60 | + |
| 61 | +Finally, you can use `setQueryData` to manually set the data of the query. This is useful when you want to refresh the query but the newest data is already available in the application. It can be used to reduce network traffic by saving network calls to the server. Or you can use it with `Mutations` to optimistically set data before the Mutation is executed & then update the query with actual data |
| 62 | + |
| 63 | +:::tip |
| 64 | +You can learn more about Optimistic Updates in the [Mutation Tutorial](/docs/basics/mutations) |
| 65 | +::: |
| 66 | + |
| 67 | +Here's an real-world example of `Query` & `QueryBuilder` |
| 68 | + |
| 69 | + |
| 70 | +```dart |
| 71 | +final anotherJob = QueryJob<String, Client>( |
| 72 | + queryKey: "another-unique-key", |
| 73 | + task: (queryKey, httpClient){ |
| 74 | + return httpClient |
| 75 | + .get("https://jsonplaceholder.typicode.com/todos/1") |
| 76 | + .then((response) => response.body);; |
| 77 | + } |
| 78 | +); |
| 79 | +
|
| 80 | +class Example extends StatelessWidget { |
| 81 | + const Example({Key? key}) : super(key: key); |
| 82 | +
|
| 83 | + @override |
| 84 | + Widget build(BuildContext context) { |
| 85 | + // getting the instance of Client provided by the [provider] package |
| 86 | + final client = Provider.of<Client>(context); |
| 87 | + |
| 88 | + return QueryBuilder<String, void>( |
| 89 | + job: job, |
| 90 | + // passing the client as externalData |
| 91 | + externalData: client, |
| 92 | + builder: (context, query) { |
| 93 | + // checking if data availability along with progressive status |
| 94 | + if (!query.hasData || query.isLoading) { |
| 95 | + return const CircularProgressIndicator(); |
| 96 | + } |
| 97 | + // remember to always show a fallback widget/screen for errors too. |
| 98 | + // It keeps the user aware of status of the application their using |
| 99 | + // & saves their time |
| 100 | + else if(query.hasError && query.isError){ |
| 101 | + return Text( |
| 102 | + "My disappointment is immeasurable & my day is ruined for this stupid error: $error", |
| 103 | + ); |
| 104 | + } |
| 105 | + return Row( |
| 106 | + children: [ |
| 107 | + Text(query.data["title"]), |
| 108 | + ElevatedButton( |
| 109 | + child: const Text("Refetch"), |
| 110 | + onPressed: () async { |
| 111 | + await query.refetch(); |
| 112 | + }, |
| 113 | + ), |
| 114 | + ], |
| 115 | + ); |
| 116 | + }, |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
0 commit comments