Create Response and Requests authored by Nicholas Sarian's avatar Nicholas Sarian
# Request
In the game manager, requests are simple classes with a constructor, a method for comparing for equality, a way to convert the object into json, and a toString method. Using the create_player_request.dart as an example, it contains 5 instance variables: name, password, crew, major, and section. The variables and the constructor are defined as follows:
```dart
final String name;
final String password;
final num crew;
final num major;
final num section;
///
/// Constructor
///
const CreatePlayerRequest({
required this.name,
required this.password,
required this.crew,
required this.major,
required this.section,
});
```
To check for equality (such as `if (request == createPlayerRequest)`), the request has a props method from Equatable that overrides the default props method:
```dart
@override
List<Object?> get props => [
name,
password,
crew,
major,
section,
];
```
When comparing two CreatePlayerRequest objects, they will be considered equal if all of the properties in props match
Inside of the request is also a method for converting the object into json format. Below is a snippet from `create_player_request.dart` showcasing how `JSON get` is used to convert the object into JSON:
```dart
JSON get asJson => {
'name': name,
'password': password,
'crew': crew.toString(),
'major': major.toString(),
'section': section.toString()
};
```
Using the create player request as an example, if you have an object like this:
```dart
final CreateplayerRequest playerRequest;
playerRequest = CreatePlayerRequest(
name: "user51092",
password: "Password1!",
crew: 1,
major: 1,
section: 1
);
```
You can convert the object into JSON using the asJson method like this:
```dart
JSON json = playerRequest.asJson;
```
# Response
Responses are used in a repository to return data from a Dio request. They include a constructor, a JSON-to-Object conversion method, a props method for comparison, and a toString method. The JSON-to-Object conversion method looks like this:
```dart
factory BasicResponse.fromJson({
required JSON json,
}) {
return BasicResponse(
success: json['success'],
description: json['description']
);
}
```
In the code above, a factory constructor is used to return an instance of the BasicResponse class. This factory constructor takes a JSON object as a parameter and returns a new BasicResponse instance with the success and description variables initialized from the JSON data.
# A Quick Note on the Constructors
There are two types of constructors you can use, one with {} and one without:
```dart
BasicResponse(this.success, this.description);
BasicResponse({required this.success, required this.description});
```
In the above example, the two constructors differ in how they are called. In the first constructor, you only have to pass in the values to the constructor, whereas the second constructor requires you to use named parameters when calling it. Named parameters help improve readability by letting the reader know what the parameters mean. Here is an example of how you would use both:
```dart
// Calling regular constructor
BasicResponse(true, "success");
// Calling constructor with named parameters
BasicResponse(success: true, description: "success")
```
In the regular constructor, the required keyword cannot be used, regardless of whether the variable can be null or not. In the named constructor, you must use the required keyword if the variable cannot be null and omit it if the variable can be null. For nullable instance variables, use the ? symbol at the end of the variable's type to indicate that it can be null.
Here's an example of an instance variable that's allowed to be null:
```dart
final bool? success
```
Variables that don't have a '?' are required and those with it can be null
\ No newline at end of file