【问题标题】:Flutter: ListView not displaying anything + lazy loadingFlutter:ListView不显示任何内容+延迟加载
【发布时间】:2023-04-11 12:34:01
【问题描述】:

我是 Flutter 的新手,正在尝试创建一个加载更多功能的列表视图。 这是我的课。它没有显示任何内容,空白屏幕。列表有我在控制台中得到结果的数据。

class ReportPurchaseNew extends StatefulWidget {
  final AdminUserDetails userDetails;
  final String title;

  const ReportPurchaseNew({Key key, this.title, this.userDetails})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => new ReportPurchaseState();
}

class ReportPurchaseState extends State<ReportPurchaseNew> {
  String fromDate = "", toDate = "", usageType =  "";
  int limit = 7, offset = 0;
  static int page = 0;


  List<Result> _List = new List();
  List<Result> _filteredList;
  Future<PurchaseReport> _PurchaseReportResponse;
  List<UsageResult> _usageList = [];
  UsageResult _usageVal;
  ScrollController _sc = new ScrollController();
  bool isLoading = false;
  //List users = new List();

  @override
  void initState() {
    this._getMorePurchase(page);
    super.initState();
    _sc.addListener(() {
      if (_sc.position.pixels ==
          _sc.position.maxScrollExtent) {
        _getMorePurchase(page);
      }
    });

  }

  @override
  void dispose() {
    _sc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Lazy Load Large List"),
      ),
      body: Container(
        child: _buildList(),
      ),
      resizeToAvoidBottomInset: false,
    );
  }

  Widget _buildList() {
    return ListView.builder(
      itemCount: _List.length + 1, // Add one more item for progress indicator
      padding: EdgeInsets.symmetric(vertical: 8.0),
      itemBuilder: (BuildContext context, int index) {
        if (index == _List.length) {
          return _buildProgressIndicator();
        } else {
          return new ListTile(
            leading: CircleAvatar(
              radius: 30.0,

            ),
            title :Text("my:"+(_List[index]
                .product)),
            subtitle: Text((_List[index]
                .unitPrice)),
          );
        }
      },
      controller: _sc,
    );
  }

  Widget _buildProgressIndicator() {
    return new Padding(
      padding: const EdgeInsets.all(8.0),
      child: new Center(
        child: new Opacity(
          opacity: isLoading ? 1.0 : 00,
          child: new CircularProgressIndicator(),
        ),
      ),
    );
  }
  Future<PurchaseReport> getProjectDetails() async {
    var result = await PurchaseReportRequest(
      context,
      widget.userDetails.result.raLoginId,
      limit.toString(),
      offset.toString(),
      fromDate,
      toDate,
      _usageVal!=null ? _usageVal.name : "" ,

    );
    return result;
  }

  void _getMorePurchase(int index) async {
    if (!isLoading) {
      setState(() {
        isLoading = true;
      });
      _PurchaseReportResponse = getProjectDetails();
      setState(() {
        isLoading = false;
        _PurchaseReportResponse.then((response) {
          if (response != null) {
            _List.addAll(response.result);
            page = page + limit;
            print("printing length : "
                +_List.length.toString());
            for (int i = 0; i < response.result.length; i++) {
              print('name:' +_List[i].product );
            }
          } else {
            errorRaisedToast(context);
          }
        });
      });
    }
  }
}

【问题讨论】:

    标签: flutter listview lazy-loading


    【解决方案1】:

    试试这个,

    if (response != null) {
    List newList = new List();
       // _List.addAll(response.result);
        page = page + limit;
        print("printing length : "
            +_List.length.toString());
        for (int i = 0; i < response.result.length; i++) {
        newList.add(response.result[i]);
          print('name:' +_List[i].product);
        }
            isLoading = false;
            _List.addAll(newList);
            page++;
         
      } else {
        errorRaisedToast(context);
      }
    

    【讨论】:

    • 同样的结果......它也显示空白屏幕
    • 在我的演示项目中,它运行良好。可能存在有关响应的问题。
    • 但我收到了回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多