Update Repository authored by Nicholas Sarian's avatar Nicholas Sarian
...@@ -30,9 +30,12 @@ Future<BasicResponse> createPlayer(CreatePlayerRequest request) async { ...@@ -30,9 +30,12 @@ Future<BasicResponse> createPlayer(CreatePlayerRequest request) async {
description: "Player could not be created. No response from server."); description: "Player could not be created. No response from server.");
} }
``` ```
The createPlayer() method returns a `Future<BasicResponse>` indicating that it is asynchronous and will return a `BasicResponse` object sometime in the future. The `await` keyword is used when calling the function (from the Bloc) to indicate that the caller should wait for the function to complete and for a future to be returned before executing other code. The createPlayer() method returns a `Future<BasicResponse>` indicating that it is asynchronous and will return a `BasicResponse` object sometime in the future. The `await` keyword is used when calling the function (from the Bloc) to indicate that the caller should wait for the function to complete and for a future to be returned before executing other code.
In the createPlayer() method, `dio.post` is used to submit data to the server for processing. The string inside dio.post specifies the endpoint that's responsible for handling the request and returning an appropriate response. The data is encoded into JSON format and sent inside the body of the request to be processed on the server-side. If dio returns with a response containing an HttpStatus.OK, the fromJson method in BasicResponse will be called with the decoded JSON data. In the createPlayer() method, `dio.post` is used to submit data to the server for processing. The string inside dio.post specifies the endpoint that's responsible for handling the request and returning an appropriate response. The endpoint specified in the string **must** match the name of the endpoint on the backend. If the name of the endpoint does not match the one on the backend exactly, the request will fail and an error response will be returned. The error response will typically indicate that either the request URL was not found or that the server returned a 404 status code.
Inside of the dio.post, once the data is encoded into JSON format, it is sent inside the body of the request to be processed on the server-side. If dio returns with a response containing an HttpStatus.OK, the fromJson method in BasicResponse will be called with the decoded JSON data.
However, the dio request also has the possibility of returning a response with a `HttpStatus.BAD_REQUEST` in it. When dio gets a response containing a bad request, it throws a DioError object containing the information about the error response. Inside the catch block, the description is retrieved from the `e` object and stored inside a string called `description`. That variable is then passed into the constructor for BasicResponse and returned. However, the dio request also has the possibility of returning a response with a `HttpStatus.BAD_REQUEST` in it. When dio gets a response containing a bad request, it throws a DioError object containing the information about the error response. Inside the catch block, the description is retrieved from the `e` object and stored inside a string called `description`. That variable is then passed into the constructor for BasicResponse and returned.
... ...
......