Created
November 22, 2021 07:18
-
-
Save gabrielgatu/ee9cfc8d10bf8b1805ae31c7d142b197 to your computer and use it in GitHub Desktop.
Flutter2Start - JSON parsing #flutter2start
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
// ignore_for_file: use_key_in_widget_constructors, prefer_const_constructors, prefer_const_literals_to_create_immutables | |
import 'package:flutter/material.dart'; | |
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
void main() { | |
runApp(App()); | |
} | |
class App extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatefulWidget { | |
@override | |
_HomePageState createState() => _HomePageState(); | |
} | |
class _HomePageState extends State<HomePage> { | |
late Future<List<StoryModel>> storyModelFuture; | |
@override | |
void initState() { | |
super.initState(); | |
storyModelFuture = downloadStoryData(); | |
} | |
Future<List<StoryModel>> downloadStoryData() async { | |
final topStoriesHttpResponse = | |
await http.get(Uri.parse("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty")); | |
final topStoriesIds = json.decode(topStoriesHttpResponse.body) as List<dynamic>; | |
final topStoriesFutures = topStoriesIds.take(10).map((storyId) async { | |
final response = | |
await http.get(Uri.parse("https://hacker-news.firebaseio.com/v0/item/$storyId.json?print=pretty")); | |
final storyData = json.decode(response.body); | |
return StoryModel.fromData(storyData); | |
}).toList(); | |
final topStories = await Future.wait(topStoriesFutures); | |
return topStories; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Colors.orange, | |
title: Text("Flutter2Start"), | |
centerTitle: true, | |
), | |
body: Center(child: body()), | |
); | |
} | |
Widget body() => FutureBuilder<List<StoryModel>>( | |
future: storyModelFuture, | |
builder: (context, snapshot) { | |
if (snapshot.connectionState != ConnectionState.done) { | |
return CircularProgressIndicator(); | |
} else { | |
return ListView.separated( | |
itemCount: snapshot.data!.length, | |
itemBuilder: (context, index) => ListTile( | |
title: Text(snapshot.data![index].title), | |
subtitle: Text(snapshot.data![index].author), | |
), | |
separatorBuilder: (context, index) => Divider(), | |
); | |
} | |
}, | |
); | |
} | |
class StoryModel { | |
final String title; | |
final String author; | |
StoryModel({ | |
required this.title, | |
required this.author, | |
}); | |
factory StoryModel.fromData(Map<String, dynamic> data) { | |
final title = data["title"]; | |
final author = data["by"]; | |
return StoryModel( | |
title: title, | |
author: author, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment