【问题标题】:Listview not showing flutterListview 不显示抖动
【发布时间】:2020-10-04 11:14:23
【问题描述】:

我有带有一些图像和详细信息的列表视图,列表视图显示虚拟数据,它工作正常,但如果我尝试调用 API 和初始化值 insteavd 的虚拟数据但列表视图甚至没有显示在屏幕上。 我也从 API 获取数据到 var restaurants = value['restaurants'][0]; 但我无法访问列表视图的数据,没有错误消息,颤振医生没有问题
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】:

    问题是:当 getRestaurantList() 没有完成时 ListView 已经渲染。您应该是用户 FutureBuilder 或在 getRestaurantList() 完成时调用 setState()!

    【讨论】:

    • 你能帮我写代码吗..? @Trần Nhật Nam Lê
    【解决方案2】:

    构建ListView时,'HomeRestauarantList'中没有数据。所以当 'HomeRestauarantList' 改变时,需要使用 setState() 重建小部件。

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

    【讨论】:

    • “getRestaurantList”的调用在哪里?您会像这样更改该代码吗? 'setState(() async { 等待 getRestaurantList(); })'
    • 我已经这样调用了 ` @override void initState() { // TODO: 实现 initState super.initState();获取餐厅列表(); }`
    • 我试过这个会导致错误setState() callback argument returned a Future.@KuKu
    • 或者你用FutureBuilder怎么样? api.flutter.dev/flutter/widgets/FutureBuilder-class.html
    • 是的,即使加载了数据,我也确实可以看到加载文本,这是相同的@KuKu
    猜你喜欢
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    相关资源
    最近更新 更多