Skip to content

Instantly share code, notes, and snippets.

@malibayram
Created October 4, 2023 15:36
Show Gist options
  • Select an option

  • Save malibayram/394913470ee95e9a206226b567a2a62e to your computer and use it in GitHub Desktop.

Select an option

Save malibayram/394913470ee95e9a206226b567a2a62e to your computer and use it in GitHub Desktop.
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Future -> Stream
// FutureBuilder -> StreamBuilder
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
/*
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit
molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
*/
class Post {
final int userId;
final int id;
final String title;
final String body;
const Post({
required this.userId,
required this.id,
required this.title,
required this.body,
});
factory Post.fromJson(Map json) {
return Post(
userId: json['userId'],
id: json['id'],
title: json['title'],
body: json['body'],
);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Stream<List<Post>> _getPosts() async* {
final posts = <Post>[];
for (int i = 10; i < 21; i++) {
final url = "https://jsonplaceholder.typicode.com/posts/$i";
final response = await http.get(Uri.parse(url));
await Future.delayed(Duration(seconds: Random().nextInt(4)));
if (response.statusCode == 200) {
posts.add(Post.fromJson(jsonDecode(response.body)));
yield posts;
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Column(
children: [
Text(
'Posts',
style: Theme.of(context).textTheme.headlineMedium,
),
Expanded(
child: StreamBuilder(
stream: _getPosts(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final posts = snapshot.data!;
return ListView(
children: [
for (final post in posts)
Card(
child: ListTile(
title: Text(post.title),
subtitle: Text(post.body),
),
),
],
);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
),
],
),
floatingActionButton: const FloatingActionButton(
onPressed: null,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment