【问题标题】:Flutter runtime Exception has occurred. _TypeError (type 'List<dynamic>' is not a subtype of type 'String')发生 Flutter 运行时异常。 _TypeError(类型 'List<dynamic>' 不是类型 'String' 的子类型)
【发布时间】:2021-07-29 16:22:04
【问题描述】:

当我尝试从 Firestore 获取数据时,会发生此异常。 _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);
        };
      }
    }

【问题讨论】:

    标签: flutter dart redux google-cloud-firestore


    【解决方案1】:

    错误是您在列表中转换字符串数据。

    检查数据库中的数据。

    这是您需要将列表更改为字符串的修复方法。

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

    【讨论】:

    • 还是不行。返回相同的错误。此外,地图的类型为 。所以传入的数据是一个列表。
    • 能否添加 StackTrace 和数据库?
    • 我的错。我在调度操作时发现这是一个问题。谢谢。
    • @kjm 如果您觉得此答案对您有所帮助,请接受该答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2019-08-18
    • 2019-09-02
    • 2021-09-12
    • 2021-02-25
    • 2020-06-21
    • 2023-01-22
    相关资源
    最近更新 更多