Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bizz84/movie_app_state_management_flutter
Flutter State Management: Movie App with Provider, Riverpod, flutter_bloc
https://github.com/bizz84/movie_app_state_management_flutter
flutter flutter-bloc provider riverpod rxdart sembast state-management
Last synced: 7 days ago
JSON representation
Flutter State Management: Movie App with Provider, Riverpod, flutter_bloc
- Host: GitHub
- URL: https://github.com/bizz84/movie_app_state_management_flutter
- Owner: bizz84
- License: mit
- Created: 2021-01-06T17:13:31.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-15T13:57:50.000Z (over 2 years ago)
- Last Synced: 2025-01-20T03:15:20.770Z (14 days ago)
- Topics: flutter, flutter-bloc, provider, riverpod, rxdart, sembast, state-management
- Language: Dart
- Homepage: https://codewithandrea.com
- Size: 1.14 MB
- Stars: 564
- Watchers: 12
- Forks: 101
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# 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:
![Movie app preview](media/app-screenshots.png)
The project uses the [TMDB API](https://www.themoviedb.org/documentation/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](#getting-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 SembastCombining 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:
- [x] [Provider](https://pub.dev/packages/provider) (using `StateNotifier`)
- [x] [flutter_bloc](https://pub.dev/packages/flutter_bloc) (using `Cubit`)
- [x] [Riverpod](https://pub.dev/packages/riverpod) (using `StateNotifier`)
- [x] [get_it](https://pub.dev/packages/get_it) and [get_it_mixin](https://pub.dev/packages/get_it_mixin)
- [x] [states_rebuilder](https://pub.dev/packages/states_rebuilder)
I plan to add more in the future (PRs welcome!).## Future Roadmap
- Add more screens
- Polish the UIFeel 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](https://pub.dev/packages/freezed) for app state and [json_annotation](https://pub.dev/packages/json_annotation) for data serialization
- [Sembast](https://pub.dev/packages/sembast) for local data persistence
- [dio](https://pub.dev/packages/dio) for the API client
- [rxdart](https://pub.dev/packages/rxdart) for combining streams
- [envied](https://pub.dev/packages/envied) for API key security
## Getting a TMDB API keyThis project uses the TMDB API to get the latest movies data.
Before running the app you need to [sign up on the TMDB website](https://www.themoviedb.org/signup), then obtain an API key on the [settings API page](https://www.themoviedb.org/settings/api).
Once you have this, create an `.env` file inside `packages/core/`, and add your key:
```dart
// .env
TMDB_KEY=your-api-key
```Then, run the code generator:
```
flutter pub run build_runner build --delete-conflicting-outputs
```This will generate a `env.g.dart` file inside `packages/core/lib/api`, that is used when making requests to the TMDB API.
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:
```
```
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`:
```
NSAppTransportSecurity
NSAllowsArbitraryLoads
```More information here:
- [Insecure HTTP connections are disabled by default on iOS and Android.](https://flutter.dev/docs/release/breaking-changes/network-policy-ios-android)
### macOS
Since macOS applications are sandboxed by default, we get a `SocketException` if we haven't added the required entitlements. This has been fixes by adding these lines to `macos/Runner/DebugProfile.entitlements` and `macos/Runner/Release.entitlements`:
```
com.apple.security.network.client```
More info here:
- [How to fix "SocketException: Connection failed (Operation not permitted)" with Flutter on macOS](https://codewithandrea.com/tips/socket-exception-connection-failed-macos/)
## Credits
This project was inspired by [flutter\_architecture\_samples](https://github.com/brianegan/flutter_architecture_samples) by the Flutter community.
## [LICENSE: MIT](LICENSE.md)