发生 Flutter 运行时异常。 _TypeError(类型 'List<dynamic>' 不是类型 'String' 的子类型)

时间:2021-07-29 16:22:04

标签: flutter dart redux google-cloud-firestore

当我尝试从 firestore 获取数据时,发生此异常。 _TypeError(“List”类型不是“String”类型的子类型) 我找不到问题的原因。请帮忙。

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 个答案:

答案 0 :(得分:0)

错误是您在列表中投射字符串数据。

检查数据库中的数据。

这是将列表更改为字符串所需的修复。

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);
        };
      }
    }