在列表中,点击的项目 ID 未正确进入 onTap 函数

In a list, the tapped item id is not coming correctly to the onTap function

我正在加载一个项目列表并按照图像将它们显示在 flutter 卡片中(我删除了一些 UI 部分)。

.

一旦用户点击卡片,它应该导航到不同的页面,并且该页面将 vehicleID 作为参数。每张卡片都有唯一的vehicleID,问题是当用户点击卡片时,传递给_onVehicleTapped方法的id是错误的,即使参数设置正确。

例如:- 如果用户点击“2011 Toyota Prius”,经过的车辆 ID 为 25,这是卡片列表中的不同车辆 ID,与点击的车辆卡片无关。

Widget build(BuildContext context) {
double deviceWidth = MediaQuery.of(context).size.width;
double deviceHeight = MediaQuery.of(context).size.height;
final ScrollController scrollController = ScrollController();
return Container(
  child: SafeArea(
    child: FutureBuilder(
        future: _getDashboardInfoList(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: Container(
                width: deviceWidth * 0.1,
                height: deviceWidth * 0.1,
                child: new CircularProgressIndicator(),
              ),
            );
          } else if (snapshot.hasError) {
            return Container(
                child: Center(
              child: Text(
                "Something Went Wrong",
                textAlign: TextAlign.center,
                style: TextStyle(color: Colors.red),
              ),
            ));
          } else {
            return Padding(
              padding: const EdgeInsets.all(6.0),
              child: Container(
                child: Scrollbar(
                  controller: scrollController,
                  child: ListView.builder(
                      controller: scrollController,
                      itemCount: snapshot.data.length,
                      scrollDirection: Axis.vertical,
                      itemBuilder: (context, index) {
                        dashboardInfo = snapshot.data[index];
                        return InkWell(
                          onTap: () => _onVehicleTapped(
                              dashboardInfo.motorVehicleId),
                          child: Container(
                            key: Key(
                                dashboardInfo.motorVehicleId.toString()),
                            margin: EdgeInsets.all(8.0),
                            child: Card(
                              elevation: 4.0,
                              margin: EdgeInsets.zero,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10),
                              ),
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                children: <Widget>[
                                  ListTile(
                                    title: Text(dashboardInfo
                                            .vehicleDisplayName
                                            .toUpperCase() +
                                        " " +
                                        dashboardInfo.motorVehicleId
                                            .toString()),
                                    key: Key('txt_vehicleDisplayName_' +
                                        index.toString()),
                                  ),
                                  dashboardInfo.uri == null
                                      ? Image(
                                          key: Key('img_vehicle_' +
                                              index.toString()),
                                          image: AssetImage(
                                              "assets/vehicle_img.png"),
                                          height: deviceHeight * 0.239,
                                          width: deviceHeight * 0.239)
                                      : Image.network(
                                          dashboardInfo.uri,
                                          key: Key('img_vehicle_' +
                                              index.toString()),
                                          height: deviceHeight * 0.239,
                                          width: deviceHeight * 0.239,
                                        ),
                                  Container(
                                    height: deviceHeight * 0.11,
                                    width: deviceWidth * 0.85,
                                    child: Card(
                                      elevation: 0.0,
                                      margin: EdgeInsets.zero,
                                      child: ListTile(
                                        leading: Container(
                                          color: Colors.blue[900],
                                          width: 40,
                                          height: 40,
                                          child: (Icon(
                                            Icons.video_label,
                                            color: Colors.white,
                                          )),
                                        ),
                                        title: Text(
                                            dashboardInfo.plateNumber == ""
                                                ? "-"
                                                : dashboardInfo.plateNumber,
                                            key: Key('txt_plateNumber_' +
                                                index.toString()),
                                            style: TextStyle(
                                                color: Colors.blue[900],
                                                fontSize: 15)),
                                        subtitle: Text(
                                          'PLATE NUMBER',
                                          style: TextStyle(
                                              color: Colors.blueGrey[400],
                                              fontSize: 14,
                                              fontWeight: FontWeight.bold),
                                        ),
                                      ),
                                    ),
                                  ),
                                  Container(
                                    height: deviceHeight * 0.11,
                                    width: deviceWidth * 0.85,
                                    child: Card(
                                      elevation: 0.0,
                                      margin: EdgeInsets.zero,
                                      child: ListTile(
                                        leading: Container(
                                          color: Colors.yellow[900],
                                          width: 40,
                                          height: 40,
                                          child: (Icon(
                                            Icons.attach_money,
                                            color: Colors.white,
                                          )),
                                        ),
                                        title: Text(
                                          utilityService
                                              .apexCurrencyConverter(
                                                  dashboardInfo
                                                      .availableBalance),
                                          style: TextStyle(
                                              color: Colors.yellow[900],
                                              fontSize: 15),
                                          key: Key('txt_availableBalance_' +
                                              index.toString()),
                                        ),
                                        subtitle: Text(
                                          'AVAILABLE BALANCE',
                                          style: TextStyle(
                                              color: Colors.blueGrey[400],
                                              fontSize: 14,
                                              fontWeight: FontWeight.bold),
                                        ),
                                      ),
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.symmetric(
                                        vertical: 5.0),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        );
                      }),
                ),
              ),
            );
          }
        }),
  ),
);

}

这是 _onVehicleTapped 方法的样子

 _onVehicleTapped(int motorVehicleId) {
Navigator.push(
    context,
    MaterialPageRoute(
        builder: (context) =>
            VehicleDetailsPage(motorVehicleId: motorVehicleId)));

}

您的变量 dashboardInfo 似乎不是局部变量。因此,它可以而且将会从一个项目到另一个项目以及您决定更改它的值的任何其他地方被覆盖。

使 dashboardInfo 成为 local 变量,例如通过添加 final 关键字:

final dashboardInfo = snapshot.data[index];

这样它将保持它的值,因为在下次调用该函数时,将生成一个 new 同名局部变量。

然后您可以删除名为 dashboardInfo 的非局部变量,无论它在哪里声明。