Search code examples
flutterdartflutter-dependenciesflutter-provider

how to add timer to the changenotifier class


I am using flutter provider package to get data.

I have 2 problems.

  1. In the DataProvider class I did initialize & it run 3 times to get data,
  2. I needs to update the data evey 5 minute(i am updating the data via calling the api) how can i excute that, i think needs to add Timer fuction.where can i add those.

`

class DataProvider with ChangeNotifier {

      DataProvider() {
        getData();
 /*this will call 3 times when i call changenotifier provider in the ui*/
      }
    
      List<Data> _vData = [];
    //<Data> is my mode
      List<Data> get vData => _vData ;
    
       getData() async {
        try {
          _vData = await instatntApi().Services();
    // got the data from service
          notifyListeners();
          return _vData ;
        } catch (e) {
          print("error provider $e");
        }
      }
    }

`

here is my ui call

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          child: ChangeNotifierProvider<DataProvider>(
            create:
                (context) => DataProvider(),
            child:  Builder(
              builder: (
                context,
              ) {
                final model = Provider.of<DataProvider>(context);

                return ListView.builder(
                    itemCount: model.vData.length,
                    itemBuilder: (BuildContext context, int index) {
                      
                      return Center(
                        child: Text(
                          model.vData[index].text.toString(),
                          style: TextStyle(color: Colors.black),
                        ),
                      );
                    });
              },
            ),
          ),
        ),
      ),
    );
  }
}

Solution

  • You can try Timer.periodic

    class DataProvider with ChangeNotifier {
       bool _call = false;
          DataProvider() {
            if(!_call){
               getData();
               _call = true;
            }
          }
        
          List<Data> _vData = [];
        
          List<Data> get vData => _vData ;
        
           getData() async {
            await processData();
             Timer.periodic(Duration(minute: 5), (t){
               processData();
             });
          }
    
          processData() async{
           try {
              _vData = await instatntApi().Services();
        // got the data from service
              notifyListeners();
            } catch (e) {
              print("error provider $e");
            }
          }
        }