列表视图未显示抖动

问题描述 投票:0回答:1

我有一些图像和详细信息的列表视图,该列表视图显示了虚拟数据,但工作正常,但如果我尝试调用API并初始化了虚假数据的值,但该列表视图甚至未显示在屏幕上。我也从API直到var restaurants = value['restaurants'][0];都获取数据,但是我无法访问列表视图中的数据,没有错误消息,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();
}
flutter dart flutter-layout
1个回答
0
投票
问题是:未固定getRestaurantList()时,ListView呈现。您应该是用户FutureBuilder或在getRestaurantList()完成时调用setState()!

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

或使用'FuretureBuilder'更新'HomeRestauarantList'的构建。或四处看看Bloc,提供者模式。

© www.soinside.com 2019 - 2024. All rights reserved.