This repository contains the source code for the tutorial State Management in Flutter: Unleashing the Power of Riverpod! published on Medium. This code demonstrates how to manage state effectively in Flutter apps using the Riverpod package, making complex state management a breeze.
- Introduction
- Features
- Getting Started
- Dependencies
- Code Overview
- License
- Contribution Guidelines
- Contact
Welcome to the Riverpod Counter App! This is a simple Flutter application demonstrating state management using Riverpod, an alternative to the popular Provider package for managing state in Flutter apps.
- 🛠 Built with Flutter and Riverpod.
- 🚀 Simple counter functionality to demonstrate state management.
- 👀 Clean separation of logic and UI using
StateProvider
andConsumerWidget
.
-
Clone this repository:
git clone https://github.com/tech-ramakant/counter_app_with_riverpod.git cd counter_app_with_riverpod
-
Install the required dependencies:
flutter pub get
-
Run the app:
flutter run
This app uses the following packages:
- flutter_riverpod: The state management solution powering this app.
- flutter: The core framework for building natively compiled applications for mobile, web, and desktop.
To add these dependencies, ensure you have the following in your pubspec.yaml
:
dependencies:
flutter:
sdk: flutter
flutter_riverpod: ^2.0.0
State Management The state is managed by Riverpod's StateProvider. This ensures that the app remains performant and easy to manage, regardless of its size and complexity.
Here’s a quick breakdown of the main components:
- counterProvider:
final counterProvider = StateProvider<int>((ref) => 0);
A simple state provider that initializes the counter to 0 and can be updated.
- ConsumerWidget:
class CounterApp extends ConsumerWidget {
The ConsumerWidget allows the UI to listen for changes in the counterProvider and update accordingly.
- UI Updates:
final count = ref.watch(counterProvider);
This line allows the widget to reactively display the current state of the counter.
- Updating State:
ref.read(counterProvider.notifier).state++;
This method increments the counter by updating the provider's state.
This project is licensed under the MIT License. Feel free to use, modify, and distribute as needed.
For more details, check out the LICENSE file.
Contributions are always welcome! If you'd like to contribute, feel free to submit a pull request or open an issue.
- Email: [email protected]
- Medium: @tech.ramakant
- LinkedIn: @tech-ramakant
- YouTube: @Tech.Ramakant
Thank you for checking out the repository! 🎉