Created
August 17, 2020 21:19
-
-
Save roipeker/5f7d8096b14c7ce2a23013986a9d9446 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
import 'dart:convert'; | |
import 'package:get/get.dart'; | |
import 'package:http/http.dart' as http; | |
class PostController extends GetxController { | |
final _postList = <PostModel>[].obs; | |
List<PostModel> get postList => _postList.value; | |
final _isLoading = false.obs; | |
bool get isLoading => _isLoading.value; | |
Future<void> getResults() async { | |
_isLoading.value = true; | |
var response = await http.get('https://jsonplaceholder.typicode.com/posts'); | |
var responseList = (jsonDecode(response.body) as List) | |
.map((e) => PostModel.fromJson(e)) | |
.toList(); | |
// auto refresh the UI... but still LOADING. | |
_postList.value = responseList; | |
_isLoading.value = false; | |
} | |
} |
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
import 'package:flutter/material.dart'; | |
import 'package:get/get.dart'; | |
import 'controller.dart'; | |
import 'post_page.dart'; | |
void main() { | |
Get.put(PostController()); | |
runApp(MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: PostPage(), | |
)); | |
} |
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
// MODEL | |
class PostModel { | |
int userId; | |
int id; | |
String title; | |
String body; | |
PostModel({this.userId, this.id, this.title, this.body}); | |
PostModel.fromJson(Map<String, dynamic> json) { | |
userId = json['userId']; | |
id = json['id']; | |
title = json['title']; | |
body = json['body']; | |
} | |
} |
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
import 'package:flutter/material.dart'; | |
import 'package:get/get.dart'; | |
import 'controller.dart'; | |
class PostPage extends GetView<PostController> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(title: Text('Posts')), | |
body: GetX( | |
init: controller, | |
initState: (_) { | |
controller.getResults(); | |
}, | |
builder: (_) { | |
if (controller.isLoading) { | |
return Center( | |
child: Text('Loading...'), | |
); | |
} | |
final postList = controller.postList; | |
if (postList.isEmpty) { | |
return Center( | |
child: Text('No content to display'), | |
); | |
} | |
// build the list. | |
return ListView.builder( | |
itemBuilder: (ctx, idx) { | |
final postItem = postList[idx]; | |
return ListTile( | |
contentPadding: EdgeInsets.all(12), | |
title: Text(postItem.title), | |
subtitle: Text(postItem.body), | |
); | |
}, | |
itemCount: postList.length, | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment