flutter state management: movie app with provider, riverpod, flutter_bloc & more
this reference project shows how to implement a (netflix-inspired) movie app with different state management techniques in flutter:
the project uses the tmdb api to fetch a list of movies, and includes features such as pagination and local storage.
running the project
before running, see instructions on how to get a tmdb api key.
also, make sure to run on flutter beta channel.
app overview
the application is composed by three primary screens: now playing, favourites and profiles.
on first launch, the app asks the user to create a profile.
the now playing page loads a list of current movies from the tmdb api. results are paginated and scrolling to the bottom causes the next page to be loaded.
each movie shows as a poster using the image url retrieved from the api. you can tap on the ❤️ icon to add a movie as a favourite (for the selected profile), and this preference is persisted to local disk.
open the favourites page to see the list of favourites for the currently selected profile.
use the profiles page to create additional profiles and update the currently selected profile (this is inspired by the netflix ui).
features
- “now playing” movies (with pagination)
- save favourites to watch list
- multiple profiles (like netflix)
- local data persistence (movies, favourites, profiles) with sembast
combining these features together makes for an interesting case study about state management in flutter.
app structure
this app was made to compare and contrast multiple state management approaches. a highly composable architecture has been designed to enable this.
the project folders are structured like this:
/apps
/flutter_bloc
/riverpod
/provider
... and more
/packages
/core
/lib
/api
/models
/app_models
/app_state
/tmdb
/persistence
/ui
each folder inside apps
is a flutter project that implements the same app with a specific state management package.
all common functionality lives in packages/core
. this includes a tmdb api wrapper, along with model classes with supporting serialization code where needed.
the persistence
folder contains a datastore
abstract class that is used by all apps, along with a concrete sembastdatastore
class to read and write data to local storage (using sembast as a nosql database).
the ui
folder contains all the custom widgets that are shared by all apps. these widgets do not hold any business logic and are designed to show the app ui and expose callbacks for the application code to plug into (much like the built-in flutter widgets).
all the logic lives inside the apps themselves. all apps have exactly the same folders:
lib
/app
/app_startup
/create_profile
/favourites
/now_playing
/profile_selection
the business logic and screen flows are identical for each app, but the semantics change depending on which state management package is used. this makes it easy to compare and constrast different solutions.
supported state management solutions
the current state management solutions are currently supported:
- provider (using
statenotifier
) - flutter_bloc (using
cubit
) - riverpod (using
statenotifier
) - states rebuilder
i plan to add more in the future (prs welcome!).
future roadmap
- add more screens
- polish the ui
- replace
http
withdio
to support request cancellation
feel free to open issues if you’d like certain features to be implemented (though keep your expectations low, i’m doing this for free after all ��).
other packages in use
the “core” package of the app uses the following packages:
- freezed for app state and json_annotation for data serialization
- sembast for local data persistence
- http for the api client
- rxdart for combining streams
getting a tmdb api key
this project uses the tmdb api to get the latest movies data.
before running the app you need to sign up on the tmdb website, then obtain an api key on the settings api page.
once you have this, create an api_keys.dart
file inside packages/core/lib/api
, and add your key:
// api_keys.dart
string tmdbapikey = "your-api-key";
congratulations, you’re good to go. ��
note: loading images from insecure http endpoints
the data returned by the tmbd api points to image urls using http rather than https. in order for images to load correctly, the following changes have been made:
android
created a file at android/app/src/main/res/xml/network_security_config.xml
with these contents:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartexttrafficpermitted="true" />
</network-security-config>
added this to the application tag in the androidmanifest.xml
:
android:networksecurityconfig="@xml/network_security_config"
ios
add the following to ios/runner/info.plist
:
<key>nsapptransportsecurity</key>
<dict>
<key>nsallowsarbitraryloads</key>
<true/>
</dict>
more information here:
credits
this project was inspired by flutter_architecture_samples by the flutter community.
Comments are closed.