小部件库捕获的异常类型“String”不是“index”颤动的“int”类型的子类型

问题描述 投票:0回答:1

当我显示列表视图生成器时,出现此错误。我不知道我做错了什么。我想我没有将任何 int 值放入 String 值。你能告诉我如何解决它吗?我在下面提供了我的代码以及我的问题的屏幕截图。 在此代码中,我获取数据库引用并基于提供的数据库引用,然后将数据添加到列表中并调用列表视图构建器来显示数据。

import 'package:bidding/global/global_variable.dart';
import 'package:bidding/screen/productDetail/product_detail.dart';
import 'package:bidding/widget/tost_message/tost_message.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';

class ProductListView extends StatefulWidget {
  final DatabaseReference dbRef;
  final String? categoty;
  const ProductListView(
      {super.key, required this.dbRef, required this.categoty});

  @override
  State<ProductListView> createState() => _ProductListViewState();
}

class _ProductListViewState extends State<ProductListView> {
  @override
  void initState() {
    super.initState();
    widget.dbRef.once().then((DatabaseEvent event) {
      if (event.snapshot.value != null) {
        Map cust = event.snapshot.value as Map;
        cust[Key] = event.snapshot.key;
        // cust.forEach((key, value) {
        (widget.categoty == "Mobile")
            ? mobileDbList.add(cust)
            : (widget.categoty == "Laptop")
                ? laptopDbList.add(cust)
                : (widget.categoty == "Car")
                    ? carDbList.add(cust)
                    : (widget.categoty == "Bike")
                        ? bikeDbList.add(cust)
                        : emptyList.add(cust);
        // });

        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: mobileDbList.isNotEmpty
          ? Expanded(
              child: ListView.builder(
                scrollDirection:
                    Axis.vertical, // Set scroll direction to horizontal
                itemCount: mobileDbList.length,

                itemBuilder: (BuildContext context, int index) {
                  return InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => ProductDetailPage(
                                  (widget.categoty == "Mobile")
                                      ? mobileDbList[index]
                                      : (widget.categoty == "Laptop")
                                          ? laptopDbList[index]
                                          : (widget.categoty == "Car")
                                              ? carDbList[index]
                                              : (widget.categoty == "Bike")
                                                  ? bikeDbList[index]
                                                  : emptyList[index],
                                )),
                      );
                    },
                    child: Container(
                      width: 200, // Set a fixed width for each item
                      margin: const EdgeInsets.only(right: 15),
                      decoration: BoxDecoration(
                        color: Colors.grey.shade200,
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            height: 100,
                            width: 200,
                            decoration: BoxDecoration(
                              color: Colors.grey.shade200,
                              borderRadius: BorderRadius.circular(20),
                            ),
                            child: FadeInImage.assetNetwork(
                              placeholder: 'assets/logo.png',
                              image: (widget.categoty == "Mobile")
                                  ? mobileDbList[index]['imageurl']
                                  : (widget.categoty == "Laptop")
                                      ? laptopDbList[index]['imageurl']
                                      : (widget.categoty == "Car")
                                          ? carDbList[index]['imageurl']
                                          : (widget.categoty == "Bike")
                                              ? bikeDbList[index]['imageurl']
                                              : emptyList[index]['imageurl'],
                              fit: BoxFit.fill,
                            ),
                          ),
                          Container(
                            margin: const EdgeInsets.only(left: 15),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                const SizedBox(
                                  height: 10,
                                ),
                                Text(
                                  "Title: ${(widget.categoty == "Mobile") ? mobileDbList[index]['title'] : (widget.categoty == "Laptop") ? laptopDbList[index]['title'] : (widget.categoty == "Car") ? carDbList[index]['title'] : (widget.categoty == "Bike") ? bikeDbList[index]['title'] : emptyList[index]['title']}",
                                  style: const TextStyle(
                                      fontSize: 15,
                                      fontWeight: FontWeight.w600),
                                ),
                                const SizedBox(
                                  height: 5,
                                ),
                                Text(
                                  "Price: ${(widget.categoty == "Mobile") ? mobileDbList[index]['price'] : (widget.categoty == "Laptop") ? laptopDbList[index]['price'] : (widget.categoty == "Car") ? carDbList[index]['price'] : (widget.categoty == "Bike") ? bikeDbList[index]['price'] : emptyList[index]['price']}",
                                  style: const TextStyle(
                                      fontSize: 15,
                                      fontWeight: FontWeight.w600),
                                ),
                                const SizedBox(
                                  height: 5,
                                ),
                                Text(
                                  "Location: ${(widget.categoty == "Mobile") ? mobileDbList[index]['location'] : (widget.categoty == "Laptop") ? laptopDbList[index]['location'] : (widget.categoty == "Car") ? carDbList[index]['location'] : (widget.categoty == "Bike") ? bikeDbList[index]['location'] : emptyList[index]['location']}",
                                  style: const TextStyle(
                                    fontSize: 15,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                },
              ),
            )
          : const Center(
              child: CircularProgressIndicator(),
            ),
    );
  }
}

这是显示的错误

这是手机屏幕的输出

这是数据库

android flutter firebase dart firebase-realtime-database
1个回答
0
投票

我认为您应该在从本地数据库填充它们后检查您的列表。之后,逐行调试语句,看看您的列表是否生成任何“列表的列表”。当列表插入到另一个列表中时,有时会出现您遇到的问题,因此当您查找特定参数的值时,它引用列表索引而不是实际值。 您可以使用 debugPrint 打印或记录日志来查看这些情况发生情况

如果是这种情况,那么只需将地图插入列表中,错误就会消失。

对代码的另一个建议是,如果该值可为空,则使用空检查运算符以避免进一步的问题。

希望有帮助

© www.soinside.com 2019 - 2024. All rights reserved.