Riverpod Fundamentals
Riverpod is a modern state management library for Flutter that stands out for its robust compile-time safety and flexibility. Unlike older solutions, Riverpod ensures that errors related to state management are caught during development, not at runtime. This leads to more reliable, maintainable code and a smoother debugging experience. By making dependencies explicit and supporting advanced features like hot-reload, Riverpod helps you build scalable Flutter apps with confidence.
main.dart
123456789101112131415161718192021222324252627282930313233343536373839import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; // Define a StateProvider for an integer counter. final counterProvider = StateProvider<int>((ref) => 0); void main() { runApp( ProviderScope( child: MyApp(), ), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: CounterScreen(), ); } } class CounterScreen extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final count = ref.watch(counterProvider); return Scaffold( appBar: AppBar(title: Text('Riverpod Counter')), body: Center( child: Text('Count: $count', style: TextStyle(fontSize: 32)), ), floatingActionButton: FloatingActionButton( onPressed: () => ref.read(counterProvider.notifier).state++, child: Icon(Icons.add), ), ); } }
In Riverpod, a provider is a way to declare a piece of state or logic that can be shared and accessed throughout your app. The counterProvider in the example above is a StateProvider, which holds a simple integer value. A consumer is any widget or function that reads and reacts to changes in a provider's value. In the CounterScreen widget, you use ref.watch(counterProvider) to listen to the counter's value, and ref.read(counterProvider.notifier).state++ to update it. This separation of state declaration and consumption makes your code more modular and testable.
main.dart
12345678910111213141516171819202122232425262728293031323334353637383940414243import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; // Define a FutureProvider that fetches a string asynchronously. final helloProvider = FutureProvider<String>((ref) async { await Future.delayed(Duration(seconds: 2)); return 'Hello from Riverpod!'; }); void main() { runApp( ProviderScope( child: MyApp(), ), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: AsyncHelloScreen(), ); } } class AsyncHelloScreen extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final asyncValue = ref.watch(helloProvider); return Scaffold( appBar: AppBar(title: Text('FutureProvider Example')), body: Center( child: asyncValue.when( data: (value) => Text(value, style: TextStyle(fontSize: 24)), loading: () => CircularProgressIndicator(), error: (err, stack) => Text('Error: $err'), ), ), ); } }
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Großartig!
Completion Rate verbessert auf 9.09
Riverpod Fundamentals
Swipe um das Menü anzuzeigen
Riverpod is a modern state management library for Flutter that stands out for its robust compile-time safety and flexibility. Unlike older solutions, Riverpod ensures that errors related to state management are caught during development, not at runtime. This leads to more reliable, maintainable code and a smoother debugging experience. By making dependencies explicit and supporting advanced features like hot-reload, Riverpod helps you build scalable Flutter apps with confidence.
main.dart
123456789101112131415161718192021222324252627282930313233343536373839import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; // Define a StateProvider for an integer counter. final counterProvider = StateProvider<int>((ref) => 0); void main() { runApp( ProviderScope( child: MyApp(), ), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: CounterScreen(), ); } } class CounterScreen extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final count = ref.watch(counterProvider); return Scaffold( appBar: AppBar(title: Text('Riverpod Counter')), body: Center( child: Text('Count: $count', style: TextStyle(fontSize: 32)), ), floatingActionButton: FloatingActionButton( onPressed: () => ref.read(counterProvider.notifier).state++, child: Icon(Icons.add), ), ); } }
In Riverpod, a provider is a way to declare a piece of state or logic that can be shared and accessed throughout your app. The counterProvider in the example above is a StateProvider, which holds a simple integer value. A consumer is any widget or function that reads and reacts to changes in a provider's value. In the CounterScreen widget, you use ref.watch(counterProvider) to listen to the counter's value, and ref.read(counterProvider.notifier).state++ to update it. This separation of state declaration and consumption makes your code more modular and testable.
main.dart
12345678910111213141516171819202122232425262728293031323334353637383940414243import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; // Define a FutureProvider that fetches a string asynchronously. final helloProvider = FutureProvider<String>((ref) async { await Future.delayed(Duration(seconds: 2)); return 'Hello from Riverpod!'; }); void main() { runApp( ProviderScope( child: MyApp(), ), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: AsyncHelloScreen(), ); } } class AsyncHelloScreen extends ConsumerWidget { @override Widget build(BuildContext context, WidgetRef ref) { final asyncValue = ref.watch(helloProvider); return Scaffold( appBar: AppBar(title: Text('FutureProvider Example')), body: Center( child: asyncValue.when( data: (value) => Text(value, style: TextStyle(fontSize: 24)), loading: () => CircularProgressIndicator(), error: (err, stack) => Text('Error: $err'), ), ), ); } }
Danke für Ihr Feedback!