Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provider .read() vs .watch()

I just updated my flutter_bloc library to 6.1.1 in which states:

bloc' is deprecated and shouldn't be used. Use context.read or context.watch instead. Will be removed in v7.0.0. Try replacing the use of the deprecated member with the replacement.

Here is a part of code that I did have to change:

class ContractSubscriptionForm extends StatelessWidget {
  final ContractSubscription contractSubscription;
  const ContractSubscriptionForm(this.contractSubscription, {Key key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => ContractSubscriptionFormBloc(
        contractSubscription,
        context.read<CoursesGroupBloc>().state.coursesGroupList,  // <---change to .read()
      ),
      child: SubscriptionFormBody(),
    );
  }
}

The part I did change is this: context.bloc<CoursesGroupBloc>().state.coursesGroupList, to context.read<CoursesGroupBloc>().state.coursesGroupList, and is now working.

In the documentation for the .read() function I read the following:

This method is the opposite of [watch]. It will not make widget rebuild when the value changes and cannot be called inside [StatelessWidget.build]/[State.build]. On the other hand, it can be freely called outside of these methods.

For some reason, this makes no sense, since the upper code is within a build of a StatelessWidget and is working with the .read() function but not with the .watch().

Am I missing something?

like image 604
Georgios Avatar asked Nov 13 '20 11:11

Georgios


People also ask

What is context watch?

context.watch<T>() listens to changes on T. context.read<T>() returns T without listening to it. You were calling. context.

What is context read?

Contexts of reading are all the elements that influence how we read in different situations. The context includes: 1) the setting, 2) the text, and 3) the purpose for reading.

When should I use provider in Flutter?

One of the main reasons to prefer Provider over Statefulwidget s is that, using Provider , you will rebuild only the widgets that needs that value (the Consumers ) while the other will not be rebuilt. Instead when you call setState the whole build function of the widget will be called.

How many types of providers are there in Flutter?

Three major components make all of this possible: the ChangeNotifier class in Flutter, the ChangeNotifierProvider (primarily used in our sample app), and the Consumer widgets.


1 Answers

you didn't call context.read inside the StatelessWidget's build, you did that inside BlocProvider during creating ContractSubscriptionFormBloc. if you try to do the following:

class example extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    context.read<CoursesGroupBloc>().state.coursesGroupList;
    //the rest of your code
  }
}

this error will appear: Tried to use context.read<bloc> inside either a build method or the update callback of a provider

like image 126
phoenix Avatar answered Oct 22 '22 00:10

phoenix