当前位置: 首页 > 知识库问答 >
问题:

发生颤振运行时异常_TypeError(类型“List”不是类型“String”的子类型)

赏成益
2023-03-14

当我试图获取数据从FIRESTAR,这个异常发生。_TypeError(类型列表不是类型字符串的子类型)我找不到问题的原因。请帮忙。

class ActfMiddleWare {
      ///Fetches LocationData From Firestore and Save it in Store;
      locationIndexMiddleware() {
        return (Store<AppState> store, action, NextDispatcher next) async {
          if (action is GetLocationIndex) {
            Future<DocumentSnapshot<Map<String, dynamic>>> locationIndex =
                FirebaseFirestore.instance
                    .collection('locations')
                    .doc('locationIndex')
                    .get();
    
            await locationIndex.then((docSnapshot) {
              if (docSnapshot.data() != null) {
                List temp = docSnapshot.data()!['locations'];
    
                store.dispatch(SaveLocationIndex(payload: temp));
              }
            });
          }
          next(action);
        };
      }
    }

共有1个答案

南宫鸿晖
2023-03-14

在列表中强制转换字符串数据时出错。

检查数据库中的数据。

下面是将列表更改为字符串所需的修复程序。

String temp = docSnapshot.data()!['locations'];

这是完整的代码

class ActfMiddleWare {
      ///Fetches LocationData From Firestore and Save it in Store;
      locationIndexMiddleware() {
        return (Store<AppState> store, action, NextDispatcher next) async {
          if (action is GetLocationIndex) {
            Future<DocumentSnapshot<Map<String, dynamic>>> locationIndex =
                FirebaseFirestore.instance
                    .collection('locations')
                    .doc('locationIndex')
                    .get();
    
            await locationIndex.then((docSnapshot) {
              if (docSnapshot.data() != null) {
                String temp = docSnapshot.data()!['locations']; -->Here is the change
    
                store.dispatch(SaveLocationIndex(payload: temp));
              }
            });
          }
          next(action);
        };
      }
    }
 类似资料: