Created
January 10, 2020 17:38
-
-
Save GIfatahTH/dfb42190f487e1fe4b5e475c34197366 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Comments extends StatelessWidget { | |
final int postId; | |
Comments(this.postId); | |
@override | |
Widget build(BuildContext context) { | |
return Injector( | |
//NOTE1: Inject CommentsService | |
inject: [Inject(() => CommentsService(api: Injector.get()))], | |
builder: (context) { | |
return StateBuilder( | |
models: [Injector.getAsReactive<CommentsService>()], | |
//NOTE2: fetch comments in the init state | |
initState: (_, commentsServiceRM) => commentsServiceRM.setState( | |
(state) => state.fetchComments(postId), | |
//NOTE3: Delegate to ErrorHandler class to show an alert dialog | |
onError: ErrorHandler.showErrorDialog, | |
), | |
builder: (_, commentsServiceRM) { | |
//NOTE4 use whenConnectionState | |
return commentsServiceRM.whenConnectionState( | |
onIdle: () => | |
Container(), //Not reachable because setState is called form initState | |
onWaiting: () => Center(child: CircularProgressIndicator()), | |
onData: (state) => Expanded( | |
child: ListView( | |
children: state.comments | |
.map((comment) => CommentItem(comment)) | |
.toList(), | |
), | |
), | |
//NOTE4: Display empty container on error. An AlertDialog should be displayed | |
onError: (_) => Container(), | |
); | |
}, | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment