Created
April 2, 2018 12:47
-
-
Save wilburx9/c3f27d8ec0abad5b338e8f0661390512 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 _MyHomePageState extends State<MyHomePage> { | |
bool _isRequestSent = false; | |
List<Post> postList = []; | |
@override | |
Widget build(BuildContext context) { | |
// This method is rerun every time setState is called | |
// | |
// The Flutter framework has been optimized to make rerunning build methods | |
// fast, so that you can just rebuild anything that needs updating rather | |
// than having to individually change instances of widgets. | |
if (!_isRequestSent) { | |
_sendRequest(); | |
} | |
return new Scaffold( | |
appBar: new AppBar( | |
// Here we take the value from the MyHomePage object that was created by | |
// the App.build method, and use it to set our appbar title. | |
title: new Text(widget.title), | |
), | |
body: new Container( | |
alignment: Alignment.center, | |
child: !_isRequestSent | |
// Request has not been sent, let's show a progress indicator | |
? new CircularProgressIndicator() | |
// Request has been sent. Show the results | |
: new Container( | |
child: new ListView.builder( | |
itemCount: postList.length, | |
scrollDirection: Axis.vertical, | |
itemBuilder: (BuildContext context, int index) { | |
return _getPostWidgets(index); | |
}), | |
), | |
), | |
); | |
} | |
void _sendRequest() async { | |
String url = "https://api.nytimes.com/svc/topstories/v2/technology" | |
".json?api-key=API_KEY_HERE"; | |
http.Response response = await http.get(url); | |
Map decode = json.decode(response.body); | |
List results = decode["results"]; | |
for (var jsonObject in results) { | |
var post = Post.getPostFrmJSONPost(jsonObject); | |
postList.add(post); | |
print(post); | |
} | |
setState(() => _isRequestSent = true); | |
} | |
Widget _getPostWidgets(int index) { | |
var post = postList[index]; | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment