我正在开发一个关于Flutter 的项目.但我在我的添加按钮主页上遇到了这样的错误.我如何解决这个问题?一旦我通过纠正空安全错误添加了一个产品,它就被修复了,但现在还没有修复.

GestureDetector(
                        onTap: () async {
                          if (_newProductGroup.text != "" && _newProductGroup.text != null) {
                            try {
                               final DocumentSnapshot<Map<String, dynamic>>
                                  _doc = await _firestore
                                      .collection("utils")
                                      .doc("productGroups")
                                      .get();
                              final List<dynamic> _tempList =
                                  _doc.data()!['list'] as List<dynamic>;
                              if (_tempList.contains(_newProductGroup.text)) {
                                showTextToast("Bu alan daha önce oluşturuldu");
                              } else {
                                _tempList.add(_newProductGroup.text);
                                _firestore
                                    .collection('utils')
                                    .doc("productGroups")
                                    .update({'list': _tempList});
                                showTextToast("Başarıyla Eklendi");
                              }
                            } catch (e) {
                              showTextToast("An Error Occured!");
                            }
                            // ignore: use_build_context_synchronously
                            Navigator.of(context).pop();
                            _newProductGroup.text = "";
                          } else {
                            showTextToast("Boş Bırakmayın!");
                          }
                        },
    Expanded(
                          child: StreamBuilder(
                            stream:
                                _firestore.collection("utils").snapshots(),
                            builder: (
                              BuildContext context,
                              AsyncSnapshot<
                                      QuerySnapshot<Map<String, dynamic>>>
                                  snapshot,
                            ) {
                              if (snapshot.hasData) {
                                final List<dynamic> _productGroups =
                                    (snapshot.data!.docs[0].data()['list'] ?? [])
                                        as List<dynamic>;
                                _productGroups.sort();
                                return GridView.builder(
                                  gridDelegate:
                                      const SliverGridDelegateWithFixedCrossAxisCount(
                                    crossAxisCount: 2,
                                    childAspectRatio: 2,
                                    crossAxisSpacing: 20,
                                    mainAxisSpacing: 20,
                                  ),
                                  itemCount: _productGroups.length,
                                  itemBuilder: (context, index) {
                                    return ProductGroupCard(                                          
                                      name: _productGroups[index] as String,
                                      key: UniqueKey(),
                                    );
                                  },
                                );

我现在已经修复了红色屏幕,但无法添加产品名称.

添加按钮

点击后错误

DB

DB

推荐答案

list字段是DB中的String,您希望它是List<dynamic>.将DB list更改为实际列表,就可以开始了.

Flutter相关问答推荐

无法在Flatter中设计正确的布局

flutter:如何将字体大小设置得更大(更大)

Firebase Auth异常未被catch捕获

如何使用底部导航栏修复此导航问题

所需引用Firebase处不存在任何对象

blocTest错误:没有从`when()`中调用方法存根

任务';:app:check DebugDuplicateClasss';的Ffltter Share_plus抛出执行失败

Flutter Android Google Sign-in error:ApiException:10在确认软件包名称、SHA-1 fingerprint 和设置同意页面后出现异常

没有为类型 'Widget' 定义运算符 '[]' .try 定义运算符[]

为 flutter app/firebase 保存 chatgpt api-key 的正确方法是什么

以编程方式在 flutter 中安装 apk 时解析错误

仅在获取(读取)、Provider 时,Firestore 中的数据为空

hooks_riverpod 有用途吗

如何从 Flutter 中的 App2 远程点击 App1 中的按钮

Flutter / 为什么当我点击我的图片时我的模型没有更新?

是否有可能减少代码的编写,例如:ref.read(countProvider.notifier) - 在构建方法中?

你如何在 Flutter 中组合两个数组?

如何在 ListView.builder 中扩展文本?

在 flutter 中使用 StreamBuilder 在文本字段中键入时显示过滤记录

Flutter:如何将列表转换为字符串?