【问题标题】:_InternalLinkedHashMap<String, dynamic> is not a subtype of type 'Map<String, String>'_InternalLinkedHashMap<String, dynamic> 不是“Map<String, String>”类型的子类型
【发布时间】:2023-04-02 17:48:01
【问题描述】:

使用下面显示的代码,我在此行收到错误(此行位于以下完整代码 sn-p 的底部):

address: data['address'] ??

我可以理解这可能意味着什么或导致什么。有人有想法吗?

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:equatable/equatable.dart';
import 'package:keekz_mvp/config/paths.dart';
import 'package:meta/meta.dart';
import 'package:keekz_mvp/models/models.dart';

class Keekz extends Equatable {
  final String id;
  final User author;
  final String imageUrl;
  final String caption;
  final int likes;
  final DateTime date;
  final GeoPoint location;
  final Map<String, String> address;

  const Keekz({
    this.id,
    @required this.author,
    @required this.imageUrl,
    @required this.caption,
    @required this.likes,
    @required this.date,
    @required this.location,
    @required this.address,
  });

  @override
  List<Object> get props => [
        id,
        author,
        imageUrl,
        caption,
        likes,
        date,
        location,
        address,
      ];

  Keekz copyWith({
    String id,
    User author,
    String imageUrl,
    String caption,
    int likes,
    DateTime date,
    GeoPoint location,
    Map<String, String> address,
  }) {
    return Keekz(
      id: id ?? this.id,
      author: author ?? this.author,
      imageUrl: imageUrl ?? this.imageUrl,
      caption: caption ?? this.caption,
      likes: likes ?? this.likes,
      date: date ?? this.date,
      location: location ?? this.location,
      address: address ?? this.address,
    );
  }

  Map<String, dynamic> toDocument() {
    return {
      'author':
          FirebaseFirestore.instance.collection(Paths.users).doc(author.id),
      'imageUrl': imageUrl,
      'caption': caption,
      'likes': likes,
      'date': Timestamp.fromDate(date),
      'location': location,
      'address': address,
    };
  }

  static Future<Keekz> fromDocument(DocumentSnapshot doc) async {
    if (doc == null) return null;
    final data = doc.data();
    final authorRef = data['author'] as DocumentReference;
    if (authorRef != null) {
      final authorDoc = await authorRef.get();
      if (authorDoc.exists) {
        return Keekz(
          id: doc.id,
          author: User.fromDocument(authorDoc),
          imageUrl: data['imageUrl'] ?? '',
          caption: data['caption'] ?? '',
          likes: (data['likes'] ?? 0).toInt(),
          date: (data['date'] as Timestamp)?.toDate(),
          location: data['location'] ?? GeoPoint(0, 0),
          address: data['address'] ??
              {
                'city': 'not set',
                'postalcode': 'not set',
                'country': 'not set'
              },
        );
      }
    }
    return null;
  }
}

【问题讨论】:

    标签: json firebase flutter dart google-cloud-firestore


    【解决方案1】:

    你可以试试这个吗?

    address: data['address']!=null ? jsonDecode(data['address']) :
    

    我认为数据来自转换为字符串的firebase,..所以它需要在将其用作 Map

    之前进行 jsondecode

    【讨论】:

    • 你不需要 json.Decode 来自 firebase 的数据。返回的值已经解码。
    【解决方案2】:

    此错误意味着地址需要 类型的映射,但它正在接收 类型的映射

    因此,请在代码的第 9 行进行更改:

    final Map&lt;String, String&gt; address;

    进入这个:

    final Map&lt;String, dynamic&gt; address;

    这意味着您的data['address'] 没有返回null,否则它会被分配not set 的默认字符串值,但实际上data['address'] 正在返回_InternalLinkedHashMap&lt;String, dynamic&gt; 类型的映射。

    应该可以解决你的问题。

    【讨论】:

    • 谢谢。不幸的是,我现在收到此错误:Unhandled Exception: type 'String' is not a subtype of type 'Map&lt;String, dynamic&gt;' in type cast
    • 您很可能没有按照指示进行操作,并且可能使用了演员 as String。因为现在它将 String 与 Map 进行比较,而一开始的情况并非如此。
    • 抱歉我的回复晚了。老实说我不知道​​为什么,但我使用完全不同的机器编译代码并重新安装.apk,因为指纹不同。上面的代码现在只使用 map ,它事先也不起作用。我不知道究竟是什么导致了这个问题。
    【解决方案3】:

    发生此错误是因为 Map 正在等待,并且您在 Address 中定义了 Map

    必须替换地址变量中的数据类型,应该如下:

    Map &lt;String, dynamic&gt; address;

    【讨论】:

    • 谢谢。不幸的是,我现在收到此错误:未处理的异常:类型“String”不是类型转换中“Map”类型的子类型
    • @SRel 我需要更多信息,只是为了确定,您将变量的定义替换为 final Map 以及“Keekz copyWith”,因为也有类似的值那里。除此之外,您还可以提供有关错误的更多信息、您具体使用了哪些信息等。
    • 在不同的机器上重新编译代码并重新安装 .apk 即可解决问题。但我不知道具体原因。
    猜你喜欢
    • 2021-10-06
    • 2019-05-02
    • 1970-01-01
    • 2021-10-08
    • 2018-12-14
    • 2018-09-24
    • 2019-04-03
    • 2019-03-23
    • 2021-01-14
    相关资源
    最近更新 更多