Skip to content

Instantly share code, notes, and snippets.

@xsahil03x
Last active November 28, 2021 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xsahil03x/0d31e3b2537cf93411f6d0f1b3f1d3b7 to your computer and use it in GitHub Desktop.
Save xsahil03x/0d31e3b2537cf93411f6d0f1b3f1d3b7 to your computer and use it in GitHub Desktop.
class ApiResponse<T> {
Status status;
T data;
String message;
ApiResponse.loading(this.message) : status = Status.LOADING;
ApiResponse.completed(this.data) : status = Status.COMPLETED;
ApiResponse.error(this.message) : status = Status.ERROR;
@override
String toString() {
return "Status : $status \n Message : $message \n Data : $data";
}
}
enum Status { LOADING, COMPLETED, ERROR }
@mattiadevivo
Copy link

Hello, I would like to ask you the meaning of the lines 6-8. Are they a particular type of method in Dart? Thank you a lot!

@xsahil03x
Copy link
Author

Hey @Devizz998, they are just factory / named constructors. You can read more about them here ->
https://dart.dev/guides/language/language-tour#named-constructors

this ->

ApiResponse.loading(this.message) : status = Status.LOADING;

is similiar to

ApiResponse.loading(this.message) {
     status = Status.LOADING;
}

@mattiadevivo
Copy link

Thank you so much, I forgot about the short notation with : .
Congratulation for your tutorial!

@happysingh23828
Copy link

Getting this error
Screenshot 2021-10-11 at 1 32 39 PM

@c01nd01r
Copy link

With null safety, it will be something like that:

class ApiResponse<T> {
  Status status = Status.LOADING;
  T? data;
  String message = '';

  ApiResponse.loading(this.message) : status = Status.LOADING;
  ApiResponse.completed(this.data) : status = Status.COMPLETED;
  ApiResponse.error(this.message) : status = Status.ERROR;

  @override
  String toString() {
    return "Status : $status \n Message : $message \n Data : $data";
  }
}

enum Status { LOADING, COMPLETED, ERROR }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment