无法访问未来构建器中的元组成员

cannot access members of tuple in future builder in flutter

我有一个元组,它有两个成员:List<DeliveryAddressModel>ApiResponseCode。代码如下:

  Future<Tuple2<List<DeliveryAddressModel>, ApiResponseCode>> getAddressList() async {
    int retry = 0;
    List<DeliveryAddressModel> deliveryAddressList = [];
    while (retry++ < 2) {
      try {
        deliveryAddressList =
            await CoreRepo.instance.getCoreClient().getDeliveryAddresses();

        for (int i = 0; i < deliveryAddressList.length; i++) {
          print("inside core repo: ${deliveryAddressList[i].addressTitle}");
        }
        return Tuple2(deliveryAddressList, ApiResponseCode.SUCCESS);
      } catch (e) {
        CoreRepo.instance.getCoreClient().getDeliveryAddresses();
      }
    }
    return Tuple2(deliveryAddressList, ApiResponseCode.SERVER_ERROR);
  }

现在我需要在其他页面中显示数据,更具体地说 List<DeliveryAddressModel>

这就是我尝试的方法:

  body: FutureBuilder(
    future: CoreRepo.instance.getAddressList(),
    builder: (context, snapshot) {
      if (snapshot.data == null) {
        return LoadingWidget(
          status: "Loading your delivery addresses",
        );
      }
      if (snapshot.data.item2 == ApiResponseCode.FAILED) { ========> ERROR 1
        return Text("Failed");
      }
      return RefreshIndicator(
        backgroundColor: Colors.black,
        child: buildFeed(snapshot.data.item1), ========> ERROR 1
        onRefresh: () => refreshList(context),
      );
    },
  ),

错误 1:

The property 'item2' can't be unconditionally accessed because the receiver can be 'null'.

Try making the access conditional (using '?.') or adding a null check to the target ('!').

这直接来自 tuple 的文档:

const t = Tuple2<String, int>('a', 10);

print(t.item1); // prints 'a' print(t.item2); // prints '10'

更多信息:core_repo, core_client, delivery_list_page

使用这个流程所以我认为解决了问题。 属性 'item2' 无法无条件访问,因为接收者可以是 'null'.

尝试使访问有条件(使用“?.”)或向目标添加空检查(“!”)。

因为快照数据本身可能为空,所以您应该安全地访问它的值

snapshot.data?.item2

你还需要修改未来的建设者

FutureBuilder<Tuple2<List<DeliveryAddressModel>, ApiResponseCode>>>

因为 future builder 无法推断你应该自己提供的数据类型