【问题标题】:Error: the operation '[]' is not defined for the type 'object'错误:未为类型“对象”定义操作“[]”
【发布时间】:2021-08-10 09:16:46
【问题描述】:

我正在使用 Null -Safety,然后在我在代码中使用快照的任何地方都不断收到此错误

这是错误

这是我的代码

StreamBuilder(
                          stream: firestore
                              .collection('interest')
                              .doc('${auth.currentUser?.uid}')
                              .snapshots(),
                          builder: (context, snapshot) {
                            if (!snapshot.hasData) {
                              return Center(
                                child: CircularProgressIndicator(),
                              );
                            }
                            return ListView.builder(
                                scrollDirection: Axis.horizontal,
                                itemCount: snapshot.data!['Interest'].length ,
                                itemBuilder: (context, index) {
                                  return Padding(
                                    padding: const EdgeInsets.only(top: 12.0),
                                    child: bottomCardList(
                                      'assets/1 (6).jpeg',
                                      snapshot.data!['Interest'][index]
                                          .toString(),
                                    ),
                                  );
                                });
                          }),

谢谢

【问题讨论】:

  • 此代码在没有 Null 安全性的情况下工作

标签: flutter dart dart-null-safety null-safety


【解决方案1】:

我通过使用类型来解决这个问题

StreamBuilder<DocumentSnapshot<Map>>(
                          stream: firestore
                              .collection('interest')
                              .doc('${auth.currentUser?.uid}')
                              .snapshots(),
                          builder: (context, snapshot) {
                            if (!snapshot.hasData) {
                              return Center(
                                child: CircularProgressIndicator(),
                              );
                            }
return ListView.builder(
                                scrollDirection: Axis.horizontal,
                                itemCount: snapshot.data!['Interest'].length ,
                                itemBuilder: (context, index) {
                                  return Padding(
                                    padding: const EdgeInsets.only(top: 12.0),
                                    child: bottomCardList(
                                      'assets/1 (6).jpeg',
                                      snapshot.data!['Interest'][index]
                                          .toString(),
                                    ),
                                  );
                                });
                          }),

【讨论】:

    【解决方案2】:

    有几个解决方案:

    • 为您的StreamBuilder 提供一个类型:

      StreamBuilder<DocumentSnapshot<Map>> (...)
      
    • builder 的第二个参数提供一个类型:

      builder: (context, AsyncSnapshot<Map> snapshot)
      
    • 使用asObject 向下转换为Map

      (snapshot.data as Map)['key']
      

    【讨论】:

      猜你喜欢
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 2020-04-02
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多