Why Choose Riverpod? - What is Riverpod

Search for a command to run...

No comments yet. Be the first to comment.
This is a series based off the topic I covered at the 2023 Devfest in Cape Town on 23 November.
This post is based off a presentation I did at Devfest 2023 in Cape Town on 23 Nov 2023. VIDEO | PRESENTATION Previously we took a look at what Riverpod is, in this post we Weill take a look at the extension methods provided by Riverpod. To help w...
The Problem Wasn’t Triage Most AI workflows in software engineering still keep the human directly in the middle of triage. The AI might help write code. It might explain a stack trace. It might summar

There’s been a steady undercurrent of people switching tools lately. Copilot to Claude. Claude to something else. Codex quietly entering more workflows. A lot of confidence in different directions, of

It’s been said to me that at times my responses can come across as a “no” first-unintentionally, of course, but real all the same. Not long ago, someone on my team shared an idea they were exploring. I began with a caution, meaning to help them avoid...

The opening sentence of any message often carries more weight than everything that follows. I’ve learned this the hard way. Recently, a report shared something he wanted to pursue. I was supportive — I really was — but my first response came as a cau...

One of the things I’ve always been comfortable with is admitting when I don’t know something. To me, it’s one of the real distinctions between the more and less experienced engineers — and honestly, people in general. When you’re earlier in your care...

This post is based off a presentation I did at Devfest 2023 in Cape Town on 23 Nov 2023. VIDEO | PRESENTATION
As described by the creator Pemi Rousselet, Riverpod is a Reactive Caching and Data-binding framework, sow while it does make for a fantastic state management solution, that is not all that it is, along with its very flexible approach to state management, it can also help with networking, dependency injection and is extremely simple when it comes to wiring test and can even help improve the overall testability of your codebase.
In many ways Riverpod is the evolution of Provider, probably one of, if not the most popular state management solution for Flutter, this means for those currently using Provider, it is extremely easy to bring Riverpod in, and also very easy to roll it into your codebase gradually.
Let's take a look at some simple state setups as examples to showcase the differences, in each of these examples we are looking at the internets favourite, counter examples.
class SimpleCounter with ChangeNotifier {
int _counter = 0;
void incrementCounter() {
_counter++;
notifyListeners();
}
void decrementCounter() {
_counter--;
notifyListeners();
}
}
class SimpleCounter extends Bloc<CounterEvents, CounterStates> {
int counter = 0;
CounterBloc() : super(InitialState()) {
on<IncrementEvent>(onIncrement);
on<DecremetnEvent>(onNumberDecrease);
}
void onIncrement(IncrementEvent event, Emitter<CounterStates> emit) async {
counter = counter + 1;
emit(UpdateState(counter));
}
void onNumberDecrease(DecremetnEvent event, Emitter<CounterStates> emit) async {
counter = counter - 1;
emit(UpdateState(counter));
}
}
As you can see above we have Provider and BLoC, many of you should be familiar with one or both of these setups. Now when we take a look at Riverpod using the StateNotifier pattern, you can also make use of the ChangeNotifer pattern which is identical to the above one from the original Provider
class SimpleCountrer extends StateNotifier<CounterState> {
CounterNotifier() : super(CounterState());
void increase() => state = state.copyWith(count: state.value + 1);
void decrease() => state = state.copyWith(count: state.value - 1);
}
Either of these patters would probably be the most common go-to's for general state management within your Flutter codebase.
Now let's take look at the simplest way one could active the same restyle using Riverpod:
final counterProvider = Provider<int>((ref) => 0);
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ProviderScope(
child: MaterialApp(
home: Scaffold(
body: Center(
child: Consumer(
builder: (context, watch, child) {
final count = watch(counterProvider);
return Text('Count: $count');
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
context.read(counterProvider).state++;
},
child: Icon(Icons.add),
),
),
),
);
}
}
Now this is something one would probably not do in real-life, however if yo look at the first line, that is basically your state, using Riverpod, the counter example can be done in a literal 1 line.
Then taking a look further down at the onPressed int eh FloatingActionButton you can see how we can simply access that state via context and increment it directly.
So if the scenario arises, one could literally store a single value in a Riverpod state, with basically zero boilerplate.
While storing a single value in state would definitely be extremely rare, it leads me into another powerful feature of Riverpod, and that is dependency injection, as Riverbeds providers are globally available and locally instantiated, the data stored in this one-liner can be accessed and updated from anywhere within your application.
Next part we will cover the extension methods that Riverpod provides.
I hope you found this interesting, and if you have any questions, comments, or improvements, feel free to drop a comment. Enjoy your development journey :D
Thanks for reading.