当我使用计时器时,FutureBuilder无法正常工作

时间:2019-12-30 11:46:02

标签: flutter flutter-layout

这是我的两种不同方法。方法getCommunitiesList正在工作,它将显示circleProgressBar,然后显示结果。函数getCommunitiesList_old不起作用,它仍然仅显示circleProgressBar,我知道问题不在FutureBuilder中。问题出在此函数内部。有人可以帮我找出问题吗?

Future<List<CommunitieModel>> getCommunitiesList()async{
  List<CommunitieModel> data=List();
  for(int i=1;i<30;i++) {
    data.add(CommunitieModel());//title:"Hi Temple $i",details: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when ...",imageUrl: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQufRouQz5g0chR_vkUnDPbYpYdorS5FtT6N9asmkp-MqhDscJ-&s"));
  }
  return data;
}

Future<List<CommunitieModel>> getCommunitiesList_old()async{
  await Timer(Duration(seconds: 5),(){
    List<CommunitieModel> data=List();
    for(int i=1;i<30;i++) {
      data.add(CommunitieModel());//title:"Hi Temple $i",details: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when ...",imageUrl: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQufRouQz5g0chR_vkUnDPbYpYdorS5FtT6N9asmkp-MqhDscJ-&s"));
    }
    return data;
  });//.tick;
}



此代码有效并由 疯狂的懒猫

Future<List<CommunitieModel>> getCommunitiesList_old() async {
  await Future.delayed(const Duration(seconds: 5));
  List<CommunitieModel> data = List();
  for (int i = 1; i < 30; i++) {
    data.add(
        CommunitieModel()); //title:"Hi Temple $i",details: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when ...",imageUrl: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQufRouQz5g0chR_vkUnDPbYpYdorS5FtT6N9asmkp-MqhDscJ-&s"));
  }
  return data;
}



我还有一个疑问
为什么此代码不起作用

Future<List<SimpleCardModel>> getTempleList()async{
  await Future.delayed(const Duration(seconds: 1),(){
    List<SimpleCardModel> data = List();
    for (int i = 11; i < 44; i++) {
      data.add(SimpleCardModel(title: "Hi Temple $i",
          details: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when ...",
          imageUrl: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQufRouQz5g0chR_vkUnDPbYpYdorS5FtT6N9asmkp-MqhDscJ-&s"));
    }
    return data;
  });


 }


1 个答案:

答案 0 :(得分:1)

尝试使用Future.delayed

Future<List<CommunitieModel>> getCommunitiesList_old() async {
  await Future.delayed(const Duration(seconds: 5));
  List<CommunitieModel> data = List();
  for (int i = 1; i < 30; i++) {
    data.add(
        CommunitieModel()); //title:"Hi Temple $i",details: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when ...",imageUrl: "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQufRouQz5g0chR_vkUnDPbYpYdorS5FtT6N9asmkp-MqhDscJ-&s"));
  }
  return data;
}