Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // pubspec.yaml
- dependencies:
- http: ^0.12.1
- flutter_redux: 0.5.0
- redux_thunk: ^0.2.1
- // appstate.dart
- class AppState {
- final List<String> persons;
- final String lastError;
- AppState({this.persons, this.lastError});
- // it's strongly recommended to override ==/hashCode
- }
- // actions.dart
- class PersonsFetchedAction {
- final List<String> persons;
- PersonsFetchedAction(this.persons);
- }
- class FetchErrorAction {
- final String error;
- FetchErrorAction(this.error);
- }
- // ajax.dart
- import 'dart:convert';
- import 'package:http/http.dart' as http;
- import 'package:guap_mobile/person.dart';
- class Ajax {
- static final baseUrl = "http://mitrakoff.com:8888/varlam";
- static Future<List<String>> fetchPersons() async {
- final headers = {"username": "Tommy", "token": "33dbd779129f406bbbb003aecd23e96f"};
- final response = await http.get("$baseUrl/person/list", headers: headers);
- if (response.statusCode == 200) {
- final personResponse = PersonResponse.fromJson(json.decode(response.body)); // you have to implement 'fromJson'
- return personResponse.getPersons();
- } else throw Exception("Failed to load persons (http code ${response.statusCode})");
- }
- }
- // thunks.dart
- import 'package:redux/redux.dart';
- import 'package:redux_thunk/redux_thunk.dart';
- import 'package:guap_mobile/redux/ajax.dart';
- import 'package:guap_mobile/redux/actions.dart';
- class Thunk {
- static ThunkAction<AppState> fetchPersons() {
- return (Store<AppState> store) async {
- try {
- store.dispatch(PersonsFetchedAction(await Ajax.fetchPersons()));
- } catch(e) {
- store.dispatch(FetchErrorAction(e.toString()));
- }
- };
- }
- }
- // reducers.dart
- import 'package:redux/redux.dart';
- import 'package:guap_mobile/redux/actions.dart';
- import 'package:guap_mobile/redux/appstate.dart';
- class AppReducer {
- static AppState personsFetchedReducer(AppState state, PersonsFetchedAction action) {
- return AppState(persons: action.persons, lastError: "");
- }
- static AppState fetchErrorReducer(AppState state, FetchErrorAction action) {
- return AppState(persons: [], lastError: action.error);
- }
- static Reducer<AppState> reducer = combineReducers<AppState>([
- TypedReducer<AppState, PersonsFetchedAction>(personsFetchedReducer),
- TypedReducer<AppState, FetchErrorAction>(fetchErrorReducer)
- ]);
- }
- // main.dart
- import 'package:flutter/material.dart';
- import 'package:flutter_redux/flutter_redux.dart';
- import 'package:redux/redux.dart';
- import 'package:redux_thunk/redux_thunk.dart';
- import 'package:guap_mobile/redux/appstate.dart';
- import 'package:guap_mobile/redux/reducers.dart';
- import 'package:guap_mobile/redux/thunks.dart';
- void main() {
- final store = new Store<AppState>(
- AppReducer.reducer,
- initialState: AppState(persons: [], lastError: ""),
- middleware: [thunkMiddleware]
- );
- runApp(MyApp(store: store));
- }
- class MyApp extends StatelessWidget {
- final Store<AppState> store;
- const MyApp({Key key, this.store}) : super(key: key);
- @override
- Widget build(BuildContext context) {
- return StoreProvider<AppState>(store: store, child: MaterialApp(
- title: "Guap",
- home: Scaffold(appBar: AppBar(
- title: Text("Guap application")),
- body: Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- children: <Widget>[
- RaisedButton(child: Text("Push me!"), onPressed: () => store.dispatch(Thunk.fetchPersons())),
- StoreConnector<AppState, AppState>(
- distinct: true,
- converter: (store) => store.state,
- builder: (context1, state) {
- return Expanded(child: ListView.builder(
- itemCount: state.lastError.isNotEmpty ? 1 : state.persons.length,
- itemBuilder: (ctxt, i) {
- final item = state.lastError.isNotEmpty ? state.lastError : state.persons[i];
- return ListTile(title: Text(item));
- })
- );
- }
- )
- ],
- )
- )
- ));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement