Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting error in riverpod statement in flutter

I have started learning flutter with a channel guide,

In the first lesson I am getting an error,

I am getting an error in the following line

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

final number=watch(provider);

and here is full code

final provider=Provider<int>((ref)=>20);

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(title: Text('RiverPod'),),
      body: Center(child: Consumer(
          builder: (context,watch,child){
            final number=watch(provider);
//getting an error in above statement, red line under watch(provider)
            return Text(number.toString());

          },
          child: Text('Riverpod')),),
    );
  }
}

>Solution :

Actually, the builder is defined something like

 builder: (BuildContext context, WidgetRef ref, Widget? child) {

So the second one provide WidgetRef and you can get .watch from this ref.

You can refactor like this to avoid misconception

builder: (context, ref, child) {
  final number = ref.watch(provider);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading