【问题标题】:how to add timer to the changenotifier class如何将计时器添加到 changenotifier 类
【发布时间】:2021-10-09 06:27:19
【问题描述】:

我正在使用 Flutter 提供程序包来获取数据。

我有 2 个问题。

  1. 在我初始化的 DataProvider 类中,它运行了 3 次 获取数据,
  2. 我需要每 5 分钟更新一次数据(我正在更新 通过调用api的数据)我怎么能执行它,我认为需要 添加计时器功能。我可以在哪里添加这些。

`

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");
        }
      }
    }

`

这是我的用户界面调用

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),
                        ),
                      );
                    });
              },
            ),
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 我需要你发布调用你的Provider 的 UI,因为这可能是三次调用的原因。您是否使用FutureBuilder 并直接在其中初始化future
  • @venir 不,我在我的 ui 中使用 changeNotifierProvider
  • @venir 现在我更新了问题中的 ui 部分
  • 你能发布你的 Provider 的完整实现吗?我们需要深入挖掘我在您的实现中没有看到的 .HomePageList.data 属性
  • 对不起,我忘了欣赏你的作品,谢谢兄弟......

标签: flutter dart flutter-dependencies flutter-provider


【解决方案1】:

你可以试试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");
        }
      }
    }

【讨论】:

  • 我能知道为什么会这样吗,期待技术回答,兄弟,你知道吗,无论如何谢谢你的回复
  • 在调用初始调用和计时器运行时仍然调用 3 - 4 次
猜你喜欢
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多