Questions tagged [flutter-provider]

For questions relating to the "Provider" package for the Flutter framework. Use this tag in combination with the general [flutter] tag. If your question applies to Flutter more generally, use the [flutter] tag only.

This tag should be used for any questions regarding the provider package for the framework.

The provider is a wrapper around InheritedWidget to make them easier to use and more reusable.

Provider uses InheritedWidget's to provide dependency injection functionality for state management in Flutter applications.

To get started, one can read through the README on GitHub, i.e. on the official GitHub page for the provider package.

1487 questions
110
votes
8 answers

When to use Provider.of vs. Consumer in Flutter

I'm still wrapping my head around state-management techniques in flutter and am a bit confused about when and why to use Provider.of vs. Consumer. I understand (I think) from the documentation that when choosing between these two you would…
Oprimus
  • 1,652
  • 3
  • 11
  • 20
109
votes
7 answers

Flutter imports: relative path or package?

In Flutter, for importing libraries within our own package's lib directory, should we use relative imports import 'foo.dart' or package import? import 'package:my_app/lib/src/foo.dart' Dart guidelines advocate to use relative imports : PREFER…
Augustin R
  • 7,089
  • 3
  • 26
  • 54
50
votes
4 answers

Provider vs. Get_it

Searching for Dependency Injection solutions for Flutter, I found two awesome libraries: provider and get_it. As far as I can see, provider has more boilerplate, but it fits really nicely with Flutter, allowing Consumer to rebuild parts of the…
42
votes
3 answers

How to use multiple Consumers for a single widget in flutter Provider

I can't wrap my head around using multiple consumers for a single widget with provider? Suppose my widget is CurvedNavigationBar and I have 4 items in that widget. I also have 4 different classes that extend ChangeNotifier and are responsible for…
Viktor Vostrikov
  • 1,322
  • 3
  • 19
  • 36
40
votes
2 answers

Why can't I use context.read in build(), but I can use Provider.of with listen: false?

It's stated in the docs that these are the same, and context.read is just a shortcut for Provider.of(context, listen: false). There's also an error in the console if I try to use context.read in a build method, but it doesn't explain the…
Sergey Molchanovsky
  • 587
  • 1
  • 4
  • 10
39
votes
3 answers

Flutter Provider setState() or markNeedsBuild() called during build

I want to load a list of events and display a loading indicator while fetching data. I'm trying Provider pattern (actually refactoring an existing application). So the event list display is conditional according to a status managed in the…
Yann39
  • 14,285
  • 11
  • 56
  • 84
32
votes
6 answers

How to use data from Provider during initState in Flutter application

I am refactoring my Flutter application code by adding Provider as a state management. Desired behavior: When Home screen opens, app should check if users email verified, if it's not then should show dialog popup. Problem: It worked fine when I…
32
votes
10 answers

How to access Provided (Provider.of()) value inside showModalBottomSheet?

I have a FloatingActionButton inside a widget tree which has a BlocProvider from flutter_bloc. Something like this: BlocProvider( builder: (context) { SomeBloc someBloc = SomeBloc(); someBloc.dispatch(SomeEvent()); return someBloc; …
Henrique Arthur
  • 988
  • 1
  • 9
  • 17
32
votes
3 answers

How to access to provider field from class that do not have context?

I am using Provider. I have got two classes: class TenderApiData {} it's stand alone class (not widget). How I can write accesstoken to AppState? class AppState extends ChangeNotifier // putted to ChangeNotifierProvider { String _accesstoken; //…
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145
32
votes
7 answers

Could not find the correct provider above this widget

I have a problem using Flutter Provider... My flow is like this: After login user id is passed to new widget -> from there it preforms save to db and then it redirects to new widget (Dashboard). And this is a code of a widget after Login: return…
harunB10
  • 4,823
  • 15
  • 63
  • 107
31
votes
10 answers

How to access Provider providers in Dialogs in Flutter

The Provider package makes use of InheritedWidget. This is a problem when I want to access a provider when I'm in a Dialog. If I load a dialog using showDialog(... builder: (context) => MyDialog); I can't access anything using InheritedWidget…
JustLearningAgain
  • 2,227
  • 4
  • 34
  • 50
27
votes
2 answers

Prevent "Tried to use Provider with a subtype of Listenable/Stream" flutter error

I want to use the provider package(4.0) in my flutter project but i get the "Tried to use Provider with a subtype of Listenable/Stream" error/warning. My constelation: I have an Api() class which contains base CRUD functionality for…
rubiktubik
  • 961
  • 2
  • 12
  • 28
25
votes
4 answers

dependOnInheritedElement() was called before initstate() in flutter

I am currently having an issue while fetching a Provider' value ininitstate`. I want to set a default value in dropdown in an Appbar and other parts in body. But I got an error saying dependOnInheritedElement() was called before initstate() in…
Brooklyn Lee
  • 369
  • 2
  • 6
  • 15
20
votes
4 answers

How to listen for change within a list using flutter provider?

I have a provider that is listening for changes to a playerList class: import 'package:flutter/material.dart'; import 'package:scam_artist/constants/PlayerColors.dart'; import 'package:scam_artist/models/Player.dart'; class PlayerList with…
quazi_moto
  • 449
  • 1
  • 3
  • 14
20
votes
2 answers

Flutter: Why is Provider a better option than a class called AppGlobal with only static singletons?

From what I understand about the flutter package, Provider, is that it's a way to share objects between widgets. I know another way of doing this is to create a class, say AppGlobal, and define various static variables that the whole app could use.…
JVE999
  • 3,327
  • 10
  • 54
  • 89
1
2 3
99 100