How to listen to changes if you are using dart only package(riverpod) or how to use Provider where there is no context?

Issue

I am learning flutter and it would be easy to understand if i get an example of listening to changes in dart only package aka riverpod.
I was using Provider in class where there is no stateless or stateful widget(basically no Buildcontext).

So i was passing context from other class to this class like this although it works but i feel its not right way.

class SaveToLocal {
  final BuildContext context;
  SaveToLocal(this.context);

 foo(){
  //used context here
  var model = Provider.of<MyList>(context, listen: false);
  }
}

then i came across riverpod where dart only package doesn’t require context but i was confused as i was not able to find things like ProviderScope.I searched also examples but most of them are on flutter_riverpod not on riverpod.
This example on pub.dev is using FutureProvider what if i wanna just fetch from model

By model i mean

class MyList extends ChangeNotifier {
  List<String> _myList = [];
  List<String> get myList => _myList;
  
  foo(){
    //some task here
    notifyListeners();
  }

So here are questions
1.Is this appropriate way of using context
2.Is there any other way that i can achieve this
3.Can i get a simple example like CounterApp where read and watch is shown?

Solution

Sorry i got confused i thought riverpod is only for dart only classes but it was for non-flutter apps(saying after reading reso coder’s article), as i tried further with flutter_riverpod and used without context (as said by Mr Random)and worked fine.

class SaveToLocal {
  final container = ProviderContainer();

  foo(){
    var model = container.read(listProvider);
 }

Answered By – Suj

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *