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

从Firestore到地图的颤振/飞镖投射

桂鑫鹏
2023-03-14

我有问题的数据映射到一个模型。我已经尝试了几个小时,总是得到铸造错误(异常),如地图不能铸造到地图或类型_InternalLinkedHashMap不是类型映射的子类型

我尝试了这些链接中的例子:

如何在Flatter中从CloudFireStore加载阵列和对象

https://github.com/dart-lang/sdk/issues/36836

还有更多的人仍然不明白这是怎么回事。

这是我的实体和模型类:

实体

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:equatable/equatable.dart';

class ScreenHomeEntity extends Equatable {
  final String id;
  final String userName;
  final String userEmail;
  final String mainDoor;
  final Map<dynamic, dynamic> doors;

  const ScreenHomeEntity(
      this.id, this.userName, this.userEmail, this.mainDoor, this.doors);

  @override
  List<Object> get props => [id, userName, userEmail, mainDoor, doors];

  @override
  String toString() =>
      'ScreenHomeEntity {id: $id, userName: $userName, userEmail: $userEmail, mainDoor: $mainDoor, doors: $doors}';

  Map<String, Object> toJson() {
    return {
      'id': id,
      'userName': userName,
      'userEmail': userEmail,
      'mainDoor': mainDoor,
      'doors': doors,
    };
  }

  static ScreenHomeEntity fromJson(Map<String, Object> json) {
    return ScreenHomeEntity(
      json['id'] as String,
      json['userName'] as String,
      json['userEmail'] as String,
      json['mainDoor'] as String,
      json['doors'] as Map<dynamic, dynamic>,
    );
  }

  Map<String, Object> toDocument() {
    return {
      'userName': userName,
      'userEmail': userEmail,
      'mainDoor': mainDoor,
      'doors': doors,
    };
  }

  static ScreenHomeEntity fromDocument(DocumentSnapshot doc) {
    return ScreenHomeEntity(
      doc.documentID,
      doc.data['userName'],
      doc.data['userEmail'],
      doc.data['mainDoor'],
      doc.data['doors'],
    );
  }
}

型号:

import 'package:brava_flutter/data/entities/screen_home_entity.dart';
import 'package:meta/meta.dart';

@immutable
class ScreenHome {
  final String id;
  final String userName;
  final String userEmail;
  final String mainDoor;
  final Map<String, ScreenHomeDoor> doors;

  ScreenHome(this.userName,
      {String userEmail = '',
      String id,
      String mainDoor,
      Map<String, ScreenHomeDoor> doors})
      : this.userEmail = userEmail ?? '',
        this.id = id,
        this.mainDoor = mainDoor ?? '',
        this.doors = doors ?? Map<String, ScreenHomeDoor>();

  ScreenHome copyWith({
    String id,
    String userName,
    String userEmail,
    String mainDoor,
    Map<String, ScreenHomeDoor> doors,
  }) {
    return ScreenHome(
      userName ?? this.userName,
      id: id ?? this.id,
      userEmail: userEmail ?? this.userEmail,
      mainDoor: mainDoor ?? this.mainDoor,
      doors: doors ?? this.doors,
    );
  }

  @override
  int get hashCode =>
      id.hashCode ^
      userName.hashCode ^
      userEmail.hashCode ^
      mainDoor.hashCode ^
      doors.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is ScreenHome &&
          runtimeType == other.runtimeType &&
          id == other.id &&
          userName == other.userName &&
          userEmail == other.userEmail &&
          mainDoor == other.mainDoor &&
          doors == other.doors;

  @override
  String toString() =>
      'ScreenHomeModel {id: $id, userName: $userName, userEmail: $userEmail, mainDoor: $mainDoor, doors: $doors}';

  ScreenHomeEntity toEntity() =>
      ScreenHomeEntity(id, userName, userEmail, mainDoor, doors);

  static ScreenHome fromEntity(ScreenHomeEntity entity) {
    return ScreenHome(
      entity.userName,
      id: entity.id,
      userEmail: entity.userEmail,
      mainDoor: entity.mainDoor,
      doors: entity.doors, // EXCEPTION
    );
  }
}

class ScreenHomeDoor {
  String name;
  String address;

  ScreenHomeDoor.fromMap(Map<dynamic, dynamic> data)
      : name = data['name'],
        address = data['address'];
}

共有1个答案

司空俊雅
2023-03-14

回答:

在模型中,方法“fromEntity”并没有将实体映射(动力学)转换为模型映射(对象),而模型映射正是异常发生的地方。这个动态地图中的每一个“值”都应该被转换成对象,而我一直在做错事。

  static ScreenHome fromEntity(ScreenHomeEntity entity) {
    return ScreenHome(
      entity.userName,
      id: entity.id,
      userEmail: entity.userEmail,
      mainDoor: entity.mainDoor,
      doors: entity.doors
          .map((k, v) => MapEntry(k as String, ScreenHomeDoor.fromMap(v))),
    );
  }

我一直试图用MapEntry(k作为String,v作为ScreenHomeDoor.fromMap(v))来做这件事

一个简单而愚蠢的错误。。。。

 类似资料:
  • 在flutter示例页面中,有一个名为“将数据发送到新屏幕”的项目。我对第65行的构造函数有一个重新保护的问题。

  • 我试图改变从API返回的日期字符串的格式。下面的日期格式输入字符串在java中工作正常,但在Dart中则不行。

  • 我已经在我的服务器上创建了一个RSA密钥对,只有服务器才会有私钥。客户端(Flatter应用程序)将访问公钥。因此,当服务器收到加密消息时,它将知道消息来自正确的客户端,并将使用其私钥解密通过公钥加密的消息。 当服务器(php7.3)向客户端发送回复时,我想在服务器端通过私钥加密消息,以便客户端可以用公钥解密消息。 非对称加密有其自身的局限性,如最大字符长度。但如果能送来就好了 Flutter包加

  • 我一直在使用VSCode进行颤振开发,但没有问题。今天,飞镖分析器突然抱怨说,大多数类都是未定义的,或者目标不存在。 有人见过这个吗? 我已经重新启动了VSCode,重新启动了计算机,卸载了flutter/dart插件,运行flutter dr没有任何问题……我没有想法了。 VSCode问题

  • [![Plugins installed][1][1]我已经在android studio中安装了这些插件,并将C:\flatter\bin添加到系统环境变量中,但当我在PowerShell中运行flatter doctor时,它显示这些插件没有安装。我用的是Windows操作系统。