代码之家  ›  专栏  ›  技术社区  ›  Alexa289

我可以使用相同的分页列表视图来显示来自不同API的结果吗?

  •  1
  • Alexa289  · 技术社区  · 4 年前

    我正在使用 infinite_scroll_pagination 包裹我有一个页面,上面有一个列表,上面有这样的搜索栏

    enter image description here

    我可以使用下面的代码获得列表

    final PagingController<int, Invoice> _pagingController =
          PagingController(firstPageKey: 0);
    
      @override
      void initState() {
        _pagingController.addPageRequestListener((pageKey) {
          _fetchPage(pageKey);
        });
        super.initState();
      }
    
      Future<void> _fetchPage(int pageKey) async {
        try {
          final newItems = await RemoteApi.getInvoiceList(pageKey, _pageSize);
          final isLastPage = newItems.length < _pageSize;
          if (isLastPage) {
            _pagingController.appendLastPage(newItems);
          } else {
            final nextPageKey = pageKey + newItems.length;
            _pagingController.appendPage(newItems, nextPageKey);
          }
        } catch (error) {
          _pagingController.error = error;
        }
      }
    

    现在我需要使用栏来实现搜索,以便从服务器中搜索单个Invoice对象。

    不幸的是,获取发票列表和获取单个发票的API是不同的。 比方说,要获取发票列表,我使用这个端点

    http://www.myapi.com/invoice/list
    

    为了获得一张发票,我使用这个端点

    http://www.myapi.com/invoice/invoiceCodeHere
    

    我能用同样的吗 PaginatedListView 从…起 无限滚动分页 包显示来自不同API的单一发票?怎么做?

    我正在尝试使用下面的代码,它确实有效,但当第一次加载数据时,它不会在中心显示圆形加载指示器

      final invoice = await invoiceAPI.getSingleInvoice(invoiceCode);
    
      pagingController.itemList = [invoice];
      pagingController.notifyListeners();
    
    0 回复  |  直到 4 年前
        1
  •  0
  •   blackwolf    4 年前

    final invoiceData=等待invoiceAPI.getSingleInvoice(invoiceCode);

    pagingController.itemList=[invoiceData]; pagingController.notifyListeners();

    pagingController.refresh();