Flutter - Provider - 将地图添加到列表替换

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

这里我需要创建一个地图列表。

我使用

provider
来保存数据。

但是当我命令

list.add()
时,它也替换了第一个元素。

这是我的代码。

我正在从 firestore 集合中获取数据。

add_inv_stream.dart

class AddInvStream extends StatelessWidget {
  const AddInvStream({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    FirebaseServices services = FirebaseServices();
    final provider = Provider.of<InventoryProvider>(context);

    return StreamBuilder<QuerySnapshot>(
      stream: services.inventory
          .where('fishType', isEqualTo: provider.inventoryData['fishType'])
          .snapshots(),
      builder: (context, snapshot) {
        if (snapshot.hasError) {
          return const Center(child: Text('Something wrong!'));
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return const Center(child: CircularProgressIndicator());
        }

        if (snapshot.data!.size == 0) {
          return const Center(
            child: Text('No inventories'),
          );
        }

        return AddInvData(snapshot: snapshot, services: services);
      },
    );
  }
}

add_inv_data.dart

class AddInvData extends StatefulWidget {
  final AsyncSnapshot<QuerySnapshot<Object?>> snapshot;
  final FirebaseServices services;

  const AddInvData({Key? key, required this.snapshot, required this.services})
      : super(key: key);

  @override
  State<AddInvData> createState() => _AddInvDataState();
}

class _AddInvDataState extends State<AddInvData> {      
  List abc = [];

  @override
  Widget build(BuildContext context) {
    final providerr = Provider.of<InventoryProvider>(context);

    return ListView.builder(
      padding: const EdgeInsets.all(15.0),
      physics: const ScrollPhysics(),
      shrinkWrap: true,
      itemCount: widget.snapshot.data!.size,
      itemBuilder: (context, index) {
        Map<String, dynamic> sellerData =
            widget.snapshot.data!.docs[index].data() as Map<String, dynamic>;

        return InkWell(
          onTap: () {
            print('Date: ${sellerData['date']} | QTY: ${sellerData['qty']}');
            showDialog(
              context: context,
              builder: (context) {
                return Dialog(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(40),
                  ),
                  elevation: 16,
                  child: Form(
                    key: _formkey,
                    child: Container(
                      padding: const EdgeInsets.all(20),
                      child: TextButton(
                        onPressed: () {
                          setState(() {
                            providerr.getInvSellerData(
                              sellerDate1: sellerData['date'],
                              sellerQty1: sellerData['qty'],
                            );
                          });
                          print(providerr.inventorySellerData);
                          abc.add(providerr.inventorySellerData);
                          print(abc);
                        },
                        child: const Text('ASSIGN'),
                      ),
                    ),
                  ),
                );
              },
            );
          },
          child: Padding(
            padding: const EdgeInsets.fromLTRB(10, 0, 10, 30),
            child: Container(
              padding: const EdgeInsets.all(15),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15),
                border: Border.all(color: Colors.black),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(sellerData['date']),
                  Text('${sellerData['qty']} kg'),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}

inv_provider.dart

class InventoryProvider with ChangeNotifier {
  Map<String, dynamic> inventorySellerData = {};

  getInvSellerData({
    String? sellerDate1,
    String? sellerQty1,
  }) {
    if (sellerDate1 != null) {
      inventorySellerData['sellerDate1'] = sellerDate1;
    }

    if (sellerQty1 != null) {
      inventorySellerData['sellerQty1'] = sellerQty1;
    }

    notifyListeners();
  }
}

我只创建了两个集合,当我点击第一个容器时,它正在打印
日期:10/31/2022 |数量:50
并且显示一个对话框。

然后我单击了正在打印的分配按钮
{卖家日期1:2022年10月31日,卖家数量1:50}
[{卖家日期1:2022年10月31日,卖家数量1:50}]

之后我点击了第二个容器,它正在打印
日期:2022 年 11 月 25 日 |数量:54
并且显示的对话框与第一个容器相同。

然后我单击了正在打印的分配按钮
{卖家日期1:2022年11月25日,卖家数量1:54}
[{sellerDate1: 11/25/2022, sellerQty1: 54}, {sellerDate1: 11/25/2022, sellerQty1: 54}]


单击分配按钮时我需要打印
[{sellerDate1: 10/31/2022, sellerQty1: 50}, {sellerDate1: 11/25/2022, sellerQty1: 54}]

我怎样才能做到这一点?

为什么要替换列表中的所有元素?

flutter google-cloud-firestore provider
1个回答
1
投票

如何将地图添加到对话框之外的列表

abc

  onTap: () {
            print('Date: ${sellerData['date']} | QTY: ${sellerData['qty']}');
            showDialog(
              context: context,
              builder: (context) {
                return Dialog(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(40),
                  ),
                  elevation: 16,
                  child: Form(
                    key: _formkey,
                    child: Container(
                      padding: const EdgeInsets.all(20),
                      child: TextButton(
                        onPressed: () {
                        // no need call setState. because you already update state by provider
                            providerr.getInvSellerData(
                              sellerDate1: sellerData['date'],
                              sellerQty1: sellerData['qty'],
                            );
                          print(providerr.inventorySellerData);

                        },
                        child: const Text('ASSIGN'),
                      ),
                    ),
                  ),
                );
              },
            );
           // add here
            abc.add(providerr.inventorySellerData);
            print(abc);
          },
© www.soinside.com 2019 - 2024. All rights reserved.