CodeBlanks Logo Icon CodeBlanks

What is Riverpod?

Learn that Riverpod is a safer alternative to Provider.

What is Riverpod?

Riverpod is the safe and testable alternative to the Provider package. Unlike Provider, it does not depend on BuildContext; it is accessed everywhere via ref. It reduces build-time errors and forces explicit distribution.

import 'package:flutter_riverpod/flutter_riverpod.dart';

The app root is wrapped with ProviderScope.

ProviderScope and ref

ProviderScope wraps the app root and manages the lifecycle of each provider. ref is the object used to access providers; it is used via ref.watch and ref.read.

void main() {
  runApp(ProviderScope(child: MyApp()));
}

Providers are declared at the top level.

Practice Import Riverpod and wrap the root

Import the flutter_riverpod package. In main, wrap the root with ProviderScope. In main, call print('riverpod hazır').

Starter code

main.dart
import 'package:flutter/material.dart';
// flutter_riverpod paketini içe aktar

void main() {
  print('riverpod hazır');
  // ProviderScope ile kök sarmala
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(body: Center(child: Text('Merhaba Riverpod'))),
    );
  }
}

Sample solution

main.dart
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

void main() {
  print('riverpod hazır');
  runApp(ProviderScope(child: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(body: Center(child: Text('Merhaba Riverpod'))),
    );
  }
}

Expected output

riverpod hazır

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What advantage does Riverpod have over the Provider package?

  • It disables hot reload
  • It removes Material widgets
  • It does not depend on BuildContext Correct answer
  • It produces a smaller APK

Explanation: Riverpod does not depend on BuildContext; it is accessed via ref from anywhere. Thus it is safer than the Provider package which depends on BuildContext.

2 Wrap the root with ProviderScope

Question
void main() {
  runApp(____(child: MyApp()));
}
Answer
void main() {
  runApp(ProviderScope(child: MyApp()));
}

Explanation: In Riverpod the app root is wrapped with ProviderScope; it manages the lifecycle of providers.

3 Riverpod parts

Term Meaning
Riverpod Safe alternative package to Provider
ProviderScope Widget wrapping the app root
ref Object to access providers
BuildContext'ten bağımsız Riverpod's core advantage

All lessons in Intermediate