![]() ![]() Import 'package:sample_flutter_redux_app/actions/actions.dart' Import 'package:flutter_redux/flutter_redux.dart' Let's call this action in a widget // lib/randomizer.dart The body of the function calls the method from our API client and when it has the response it dispatches one of our normal actions that we already had. MyBoxColor boxColor = await ApiClient.getBoxColor() Īs you can see our thunk action returns a function that receives the store. Import 'package:sample_flutter_redux_app/models/api_client.dart' Import 'package:redux_thunk/redux_thunk.dart' Now we can add the thunk action to our actions file: // lib/actions/actions.dart To read more about JSON and serialization in Flutter here is the official documentaion: fromJson constructor is used as a method of deserializing JSON data in our model. Our API client doesn't make any http requests but it creates a JSON object with random values for different colors which are used to create a MyBoxColor instance.Īt this point the IDE should complain that MyBoxColor has no fromJson constructor. Creating an instance from decoded JSON 'blue': (rng.nextDouble() * 101).floorToDouble() / 10,Īwait layed(Duration(milliseconds: 500)) Random values are doubles in range with 0.1 step The data is JSON encoded only for demonstration Import 'package:sample_flutter_redux_app/models/models.dart' Let's demonstrate that by creating a mock API client. One of the most common async calls example is the http request where we await a response from an API for example and we dispatch an action with a payload build from the response. Import 'package:redux_thunk/redux_thunk.dart' // Import package Now we can add the middleware to the store: // lib/main.dart Redux_thunk: ^0.2.1 # Use the latest version Redux thunk middleware is a package that we need to add to our app separately: # pubspec.yaml Let's see all that in the code: Redux Thunk middleware This middleware receives the returned functions from the thunk actions and executes them. Handling this special kinf of actions needs a middleware called "Redux Thunk" middleware. ![]() These kind of action creators are called thunk actions and the function that is returned by them can contain the side effects that we need. We can have another kind of action creators which return a function instead of creating objects. Until now our actions were simple objects created by an action class. The only place remaining is in actions and this part is all about that. ![]() Nor can we use reducers for that since they're pure functions that are not allowed to have any side effects. ![]() Surely we don't want to do that in our widgets. Usually one of first topics that comes up when getting started with Redux is the question of how to handle side effects such as async calls and conditional flows in our state management. Source code: /hybridheroes/flutter_redux_example_app Redux Thunk In this part we're gonna build on top of the sample app from the first part which you can find here:Īrticle: Part 1 - Redux setup and basic state management In the previous part of this article we went through the basics of Redux usage in Flutter by building a simple app that managed its state in a Redux store. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |