Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Creating and Registering Providers | Provider Essentials
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Flutter State Management Fundamentals

bookCreating and Registering Providers

When you register a provider, you make data available to the widget subtree below the provider. In the example above, the ChangeNotifierProvider is placed at the root of the app, which means any widget in the app can access the Counter instance. Widgets can access the provided data using methods like Provider.of<T>(context), context.watch<T>(), or context.read<T>(). This allows widgets to listen for changes and rebuild only when necessary, making your app more efficient and responsive.

main.dart

main.dart

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; // A simple ChangeNotifier class for demonstration class Counter extends ChangeNotifier { int value = 0; void increment() { value++; notifyListeners(); } } void main() { runApp( // Registering the ChangeNotifierProvider at the root of the app ChangeNotifierProvider( create: (context) => Counter(), child: const MyApp(), ), ); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Provider Demo', home: const CounterScreen(), ); } } class CounterScreen extends StatelessWidget { const CounterScreen({super.key}); @override Widget build(BuildContext context) { // Accessing the provided Counter instance final counter = Provider.of<Counter>(context); return Scaffold( appBar: AppBar( title: const Text('Counter Example'), ), body: Center( child: Text( 'Value: [${counter.value}[', style: const TextStyle(fontSize: 24), ), ), floatingActionButton: FloatingActionButton( onPressed: counter.increment, child: const Icon(Icons.add), ), ); } }
question mark

Which provider type should you use to expose a class that notifies listeners when its state changes?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain the difference between `context.watch<T>()` and `context.read<T>()`?

How does `ChangeNotifierProvider` work with multiple providers?

Can you give an example of how to update the provided data?

bookCreating and Registering Providers

Deslize para mostrar o menu

When you register a provider, you make data available to the widget subtree below the provider. In the example above, the ChangeNotifierProvider is placed at the root of the app, which means any widget in the app can access the Counter instance. Widgets can access the provided data using methods like Provider.of<T>(context), context.watch<T>(), or context.read<T>(). This allows widgets to listen for changes and rebuild only when necessary, making your app more efficient and responsive.

main.dart

main.dart

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; // A simple ChangeNotifier class for demonstration class Counter extends ChangeNotifier { int value = 0; void increment() { value++; notifyListeners(); } } void main() { runApp( // Registering the ChangeNotifierProvider at the root of the app ChangeNotifierProvider( create: (context) => Counter(), child: const MyApp(), ), ); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Provider Demo', home: const CounterScreen(), ); } } class CounterScreen extends StatelessWidget { const CounterScreen({super.key}); @override Widget build(BuildContext context) { // Accessing the provided Counter instance final counter = Provider.of<Counter>(context); return Scaffold( appBar: AppBar( title: const Text('Counter Example'), ), body: Center( child: Text( 'Value: [${counter.value}[', style: const TextStyle(fontSize: 24), ), ), floatingActionButton: FloatingActionButton( onPressed: counter.increment, child: const Icon(Icons.add), ), ); } }
question mark

Which provider type should you use to expose a class that notifies listeners when its state changes?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 2
some-alt