The named parameter 'bloc' isn't defined - bloc provider - flutter

I've read the flutter-bloc documentation and I really have no idea what to do anymore. Someone to help?
error bloc provider

Probably you didn't import the Bloc package into this file.

Try create: instead of bloc:
BlocProvider<UserBloc>(
create: (context) => UserBloc(),
child: Container(),
);

Related

How to pass an existing repository instance to the next page in Flutter using the flutter_bloc package?

I have the following code that passes a bloc to the next page
Navigator.of(context).push(
MaterialPageRoute<CreateFeedSelectClass>(
builder: (_) => BlocProvider.value(
value: BlocProvider.of<TeacheractivityfeedBloc>(context),
child: CreateFeedSelectClass(imageStringList: state.selectedImages, imagePathList: state.selectedImagePaths, classList: state.classList,))
)
);
I would like to pass this repository that was already created in this page before the build function
TeacherRepository _teacherRepository = TeacherRepository();
In the example from flutter bloc (https://pub.dev/packages/flutter_bloc)
there is only a way to create a new instance, I want to pass the existing instance instead of creating a new one:
RepositoryProvider(
create: (context) => RepositoryA(), //----->
child: ChildA(),
);
instead of
create: is there any way to do something like value: _teacherRepository like how we do it with BlocProvider.value(value:....) ?
Actually there is a named constructor value available for RepositoryProvider which you can use.
RepositoryProvider.value(
value: repository,
child: Container(),
);

Confusion about the usage of angle brackets in flutter

I am learning flutter right now and I am reading some tutorial code. I know angle brackets e.g. refer to a set of widgets but how I should understand follow code then:
return Center(
child: ChangeNotifierProvider<CartModel>(
data: CartModel(),
child: Builder(builder: (context) {...}
This is a set of CartModel? but then what does the ChangeNotifierProvider mean here?
The ChangeNotifierProvider is there to listen to the notifications of the CartModel Provider.
The CartModel is the type of provider that the ChangeNotifierProvider will listen to for updates.
ChangeNotifierProvider is the constructor with the argument.
data: CartModel(),
child: Builder(builder: (context) {...}
which will call the build method of class ChangeNotifierProvider.

ChangeNotifierProxyProvider builder vs update issue

i was using provider: ^3.0.0 in which it has ChangeNotifierProxyProvider parameters 'builder' in which i was providing a data. Now provider version is changed and now its updated version is provider: ^4.0.4 and it does not have 'builder' function now it has create and update functions. Kindly tell me, what's the logic behind the create and update.
I think crates runs only first time and updates runs every time after first time?
Am i right! But i have an issue, i have to pass some data to the next class in a parameter.
See my code
ChangeNotifierProxyProvider<Auth, Shops>(
create: (ctx)=> Shops('778b2f743f2aebd4d73d2431881a88ba54c53c01', []),
update: (ctx, auth, prevShop)=> Shops(auth.token, prevShop.items),
),
I wanna pass the authToken to the Shop Class, in update this data can be fetched from that auth object but in create what should i do?
And i wanna pass the list of shops as a second argument in shop class, which will be the list of Shops fetched from my server, but here because i don't have and reference so i have to pass an empty list in create.
Which means when page is loaded first time there were no list of shops and when i again open same page then the shop list will be listed on the screen.
kindly help me out to clear this issue.
Do it like this
ChangeNotifierProxyProvider<Auth, Shops>(
create: (ctx) => Shops('', []),
update: (ctx, auth, prevShop) => Shops(
auth.token.toString(),
prevShop == null ? [] : prevShop.items)
),
How about doing this
ChangeNotifierProxyProvider<Auth, Shops?>(
create: (_) => null,
update: (ctx, auth, prevShop) => Shops(
auth.token.toString(),
prevShop == null ? [] : prevShop.items)
),
I also encountered the same problem. I searched the documentation and found the solution.
You need to define the function update() in the Shops() class which takes in your arguments:
auth.token and prevShop.items in that and you need to update anything you want in that update function.
ChangeNotifierProxyProvider<Auth, Shops>(
create: (ctx)=> Shops(),
update: (ctx, auth, prevShop)=> prevShop..update(auth, prevShop.items),
),
We use ..update(auth.token, prevShop.items) to return the same Shops() widget.

How can I dispose ChangeNotifierProvider<T>.value( ) in Flutter

how can I dispose ChangeNotifierProvider.value( ) in Flutter
so there will be no memory leaks
I made the object that is as a value to the ChangeNotifierProvider.value( ) as a singleton
cause I need more than page to share their state in one object
ChangeNotifierProvider automatically calls the dispose() method of the state when it is needed. You need not do anything about that. They talk about this in this video:Pragmatic State Management in Flutter (Google I/O'19)
You need to use the dispose() method of State or the default constructor of ChangeNotifierProvider. The latter automatically disposes of the object created in the create function.
I wonder why you are using ChangeNotifierProvider.value() instead of ChangeNotifierProvider(), but assuming you want to pass the value to a different page, you can combine both as follows.
ChangeNotifierProvider<Foo>(
create: (context) => Foo(), // this Foo object is disposed of automatically
)
Somewhere down the tree:
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (context2) => Provider<Foo>.value(
value: Provider.of<Foo>(context, listen: false),
child: NextPage(),
),
),
)
Note: You won't have to do this if you provide the value from above MaterialApp.

How to scope a ChangeNotifier to some routes using Provider?

I have a ChangeNotifier, and I would like to share it between multiple routes but not all routes:
Page1 is my first page. I need share data of ChangeNotifierProvider with Page2, Page3 and Page only and on enter Page1 call dispose of my ChangeNotifierProvider.
How can I do this using provider?
To do so, the easiest solution is to have one provider per route, such that instead of:
Provider(
builder: (_) => SomeValue(),
child: MaterialApp(),
)
you have:
final value = SomeValue();
MaterialApp(
routes: {
'/foo': (_) => Provider.value(value: value, child: Foo()),
'/bar': (_) => Provider.value(value: value, child: Bar()),
'/cannot-access-provider': (_) => CannotAccessProvider(),
}
)
It is, on the other hand, not possible to have your model "automatically disposed".
provider is not able in such a situation to know that it is safe to dispose of the object.