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:async'; | |
import 'package:flutter/material.dart'; | |
import 'package:bloc/bloc.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
class SimpleBlocDelegate extends BlocDelegate { | |
@override | |
void onTransition(Transition transition) { |
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'; | |
void main() { | |
runApp( | |
MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('Example'), | |
), | |
body: PageA(), |
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
name: flutter_infinite_list | |
description: A new Flutter project. | |
version: 1.0.0+1 | |
environment: | |
sdk: ">=2.0.0 <3.0.0" | |
dependencies: | |
flutter: |
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
[ | |
{ | |
"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" | |
}, | |
{ | |
"userId": 1, | |
"id": 2, |
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:equatable/equatable.dart'; | |
class Post extends Equatable { | |
final int id; | |
final String title; | |
final String body; | |
Post({this.id, this.title, this.body}) : super([id, title, body]); | |
@override |
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:equatable/equatable.dart'; | |
abstract class PostEvent extends Equatable {} | |
class Fetch extends PostEvent { | |
@override | |
String toString() => 'Fetch'; | |
} |
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:equatable/equatable.dart'; | |
import 'package:flutter_infinite_list/models/models.dart'; | |
abstract class PostState extends Equatable { | |
PostState([List props = const []]) : super(props); | |
} | |
class PostUninitialized extends PostState { | |
@override |
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:bloc/bloc.dart'; | |
import 'package:meta/meta.dart'; | |
import 'package:http/http.dart' as http; | |
import 'package:flutter_infinite_list/bloc/bloc.dart'; | |
import 'package:flutter_infinite_list/models/models.dart'; | |
class PostBloc extends Bloc<PostEvent, PostState> { | |
final http.Client httpClient; | |
PostBloc({@required this.httpClient}); |
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:bloc/bloc.dart'; | |
import 'package:meta/meta.dart'; | |
import 'package:http/http.dart' as http; | |
import 'package:flutter_infinite_list/bloc/bloc.dart'; | |
import 'package:flutter_infinite_list/models/models.dart'; | |
class PostBloc extends Bloc<PostEvent, PostState> { | |
final http.Client httpClient; | |
PostBloc({@required this.httpClient}); |
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
@override | |
Stream<PostState> mapEventToState(PostEvent event) async* { | |
if (event is Fetch && !_hasReachedMax(currentState)) { | |
try { | |
if (currentState is PostUninitialized) { | |
final posts = await _fetchPosts(0, 20); | |
yield PostLoaded(posts: posts, hasReachedMax: false); | |
} | |
if (currentState is PostLoaded) { | |
final posts = await _fetchPosts(currentState.posts.length, 20); |