Skip to content

Instantly share code, notes, and snippets.

@bardiarastin
Created January 26, 2020 13:26
Show Gist options
  • Save bardiarastin/1ea6d5969e7a695980165818432c2512 to your computer and use it in GitHub Desktop.
Save bardiarastin/1ea6d5969e7a695980165818432c2512 to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'package:redux/redux.dart';
import 'package:meta/meta.dart';
import 'package:redux_example/src/models/i_post.dart';
import 'package:redux_example/src/redux/posts/posts_state.dart';
import 'package:redux_example/src/redux/store.dart';
import 'package:http/http.dart' as http;
@immutable
class SetPostsStateAction {
final PostsState postsState;
SetPostsStateAction(this.postsState);
}
Future<void> fetchPostsAction(Store<AppState> store) async {
store.dispatch(SetPostsStateAction(PostsState(isLoading: true)));
try {
final response = await http.get('https://jsonplaceholder.typicode.com/posts');
assert(response.statusCode == 200);
final jsonData = json.decode(response.body);
store.dispatch(
SetPostsStateAction(
PostsState(
isLoading: false,
posts: IPost.listFromJson(jsonData),
),
),
);
} catch (error) {
store.dispatch(SetPostsStateAction(PostsState(isLoading: false)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment