firebase 来自Map的Flutter射击基地

szqfcxe2  于 2022-12-30  发布在  Flutter
关注(0)|答案(2)|浏览(101)

我在firebase数据库中转换数组属性时遇到问题。
我在firebase中有一个数据库,里面有一些“faq”。faq的一些属性是字符串数组

我尝试将firebase从map转换为对象,如下所示:

我得到一个错误:

该问题与模型的“标记”和“图像”属性有关。
有没有人能解释一下为什么它失败了,以及我如何纠正这个问题。
完整型号分类如下:

import 'dart:convert';

import 'package:collection/collection.dart';

class Faq {
  String faqId;
  String? question;
  String? answer;
  List<String>? tags;
  List<String>? images;

  Faq(
      {required this.faqId,
      this.question,
      this.answer,
      this.tags,
      this.images});

  @override
  String toString() {
    return 'Faq(faqId: $faqId, question: $question, answer: $answer, tags: $tags, images: $images)';
  }

  factory Faq.fromMap(Map<String, dynamic> data) => Faq(
        faqId: data['faqId'] as String,
        question: data['question'] as String?,
        answer: data['answer'] as String?,
        tags: data['tags'] as List<String>?,
        images: data['images'] as List<String>?,
      );

  Map<String, dynamic> toMap() => {
        'faqId': faqId,
        'question': question,
        'answer': answer,
        'tags': tags,
        'images': images,
      };

  /// `dart:convert`
  ///
  /// Parses the string and returns the resulting Json object as [Faq].
  factory Faq.fromJson(String data) {
    return Faq.fromMap(json.decode(data) as Map<String, dynamic>);
  }

  /// `dart:convert`
  ///
  /// Converts [Faq] to a JSON string.
  String toJson() => json.encode(toMap());

  Faq copyWith({
    required String faqId,
    String? question,
    String? answer,
    List<String>? tags,
    List<String>? images,
  }) {
    return Faq(
      faqId: faqId,
      question: question ?? this.question,
      answer: answer ?? this.answer,
      tags: tags ?? this.tags,
      images: images ?? this.images,
    );
  }

  @override
  bool operator ==(Object other) {
    if (identical(other, this)) return true;
    if (other is! Faq) return false;
    final mapEquals = const DeepCollectionEquality().equals;
    return mapEquals(other.toMap(), toMap());
  }

  @override
  int get hashCode =>
      faqId.hashCode ^
      question.hashCode ^
      answer.hashCode ^
      tags.hashCode ^
      images.hashCode;
}
62lalag4

62lalag41#

当你解析你的回应时,试试这个:

factory Faq.fromMap(Map<String, dynamic> data) => Faq(
    faqId: data['faqId'] as String,
    question: data['question'] as String?,
    answer: data['answer'] as String?,
    tags: List<String>.from(data["tags"] ?? []); // <--- change this
    images: data['images'] as List<String>?,
);
fykwrbwg

fykwrbwg2#

请在fromMap()中的tags中尝试此操作:

// ...
tags: (data["tags"] as List).map((elem) => elem.toString()).toList(),

因此新方法将是:。

factory Faq.fromMap(Map<String, dynamic> data) => Faq(
        faqId: data['faqId'] as String,
        question: data['question'] as String?,
        answer: data['answer'] as String?,
        tags: (data["tags"] as List).map((elem) => elem.toString()).toList(),            
        images: data['images'] as List<String>?,
      );

相关问题