列表视图未显示抖动

时间:2020-06-15 04:47:03

标签: flutter dart flutter-layout

我有一些图像和详细信息的列表视图,该列表视图显示了虚拟数据,它工作正常,但是如果我尝试调用API并初始化了虚假数据的值,但是该列表视图甚至没有显示在屏幕上。 另外,我正在从var restaurants = value['restaurants'][0];以上的API获取数据,但无法访问列表视图中的数据,没有错误消息,在flutter doctor中也没有问题
API调用

Future<String> getRestaurantList() async {
    ProgressDialog dialog = CustomDialogs().showLoadingProgressDialog(context);

    var response = await http.post(
      Urls.HOME_RESTAURANTS,
      headers: {"Content-Type": "application/json"},
      body: json.encode({
        "line1": "lat_11.2717278\$75.7775994",
        "category": "all",
        "term": "null",
      }),
    );
    Map<String, dynamic> value = json.decode(response.body);

    if (response.statusCode == 200) {
      dialog.dismissProgressDialog(context);
      try {
        print("response " + response.body.toString());
        var message = value['msg'];
        var rescount = value['count'];
        if (message == "Exist") {
          var restaurants = value['restaurants'][0];
          for (int i = 0; i < restaurants.length; i++) {
            var data = restaurants[i];
            print("response"+ data);
            HomeRestauarantList.add(HomeRestauarantModel.fromJson(data));
          }

        } else {
          final snackBar = SnackBar(
              content: Text("No Restaurants Available in the Location"));
          _scaffoldKey.currentState.showSnackBar(snackBar);
        }
      } catch (e) {
        e.toString();
        print(e.toString());
      }
    }
  }

查看

  Widget restList = Container(
    width: double.infinity,
    child: Column(
      children: <Widget>[
        Container(
          child: ListView.separated(
              physics: const NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              separatorBuilder: (BuildContext context, int index) =>
                  const Divider(),
              itemCount: HomeRestauarantList.length,
              itemBuilder: (BuildContext context, int index) {
                HomeRestauarantModel data = HomeRestauarantList[index];
                return Container(
                  width: double.infinity,
                  child: GestureDetector(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => RestaurantScreen()),
                      );
                    },
                    child: Padding(
                      padding: EdgeInsets.all(16),
                      child: Container(
                        child: Row(
                          children: <Widget>[
                            Expanded(
                              flex: 0,
                              child: Image.network(
                                data.logo,
                                height: 80,
                                fit: BoxFit.fitWidth,
                              ),

                              //radius: 52.5,
                            ),
                            Expanded(
                              flex: 1,
                              child: Padding(
                                padding: EdgeInsets.all(16),
                                child: Row(
                                  children: <Widget>[
                                    Expanded(
                                      flex: 0,
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Padding(
                                            padding: EdgeInsets.all(0),
                                            child: Text(
                                              data.name,
                                              style: TextStyle(
                                                fontSize: 15,
                                                color: Colors.black,
                                              ),
                                              textAlign: TextAlign.left,
                                            ),
                                          ),
                                          Padding(
                                            padding: EdgeInsets.only(top: 5),
                                            child: Text(
                                              data.tag_line,
                                              style: TextStyle(
                                                fontSize: 12,
                                                color: Colors.grey,
                                              ),
                                              textAlign: TextAlign.left,
                                            ),
                                          ),
                                          Padding(
                                            padding: EdgeInsets.only(top: 5),
                                            child: Text(
                                              "20% Offer above ₹130",
                                              style: TextStyle(
                                                fontSize: 12,
                                                fontWeight: FontWeight.bold,
                                                color: const Color(0xFFCBB032),
                                              ),
                                              textAlign: TextAlign.left,
                                            ),
                                          ),

                                        ],
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                );
              }),
        ),
      ],
    ),
  );

型号

class HomeRestauarantModel {
  String res_id;
  String name;
  String logo;
  String status;
  String tag_line;
  String min_prepration_time;

  HomeRestauarantModel({
    this.res_id,
    this.name,
    this.logo,
    this.status,
    this.tag_line,
    this.min_prepration_time,
  });

  HomeRestauarantModel.fromJson(json)
      : res_id = json['res_id'].toString(),
        name = json['name'],
        logo = json['logo'].toString(),
        status = json['status'].toString(),
        tag_line = json['tag_line'].toString(),
        min_prepration_time = json['min_prepration_time'].toString();
}

2 个答案:

答案 0 :(得分:0)

问题是:当getRestaurantList()不固定时,ListView会呈现。您应该是FutureBuilder用户,或者在getRestaurantList()完成时调用setState()!

答案 1 :(得分:0)

构建ListView时,“ HomeRestauarantList”中没有数据。因此,当“ HomeRestauarantList”更改时,需要使用setState()进行小部件重建。

或者使用“ FuretureBuilder”更新“ HomeRestauarantList”的构建。 或四处看看Bloc,Provider模式。