ไม่สามารถกำหนดประเภทอาร์กิวเมนต์ 'Map <String, dynamic> Function ()' ให้กับประเภทพารามิเตอร์ 'Map <String, dynamic>'

Aug 26 2020

ตอนแรกสิ่งนี้สามารถใช้งานได้ แต่หลังจากการอัปเดต firebase ตอนนี้ทำให้ฉันเกิดข้อผิดพลาดนี้ ฉันได้เพิ่มเครื่องหมายดอกจันในส่วนที่ให้ข้อผิดพลาด เพิ่มข้อความแสดงข้อผิดพลาดใต้รหัสแล้ว

import 'package:cloud_firestore/cloud_firestore.dart';

class Record {
  final String name;
  final int totalVotes;
  final DocumentReference reference;

  Record.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map['name'] != null),
        assert(map['totalVotes'] != null),
        name = map['name'],
        totalVotes = map['totalVotes'];

  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(**snapshot.data**, reference: snapshot.reference);

  @override
  String toString() => "Record<$name:$totalVotes>";
}

class Voters {
  String uid;
  String voteId;
  String markedVoteOption;
}

ไม่สามารถกำหนดประเภทอาร์กิวเมนต์ 'Map <String, dynamic> Function ()' ให้กับประเภทพารามิเตอร์ 'Map <String, dynamic>'

คำตอบ

15 PeterHaddad Aug 26 2020 at 19:23

ลองทำดังต่อไปนี้:

  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data(), reference: snapshot.reference);

data() เป็นวิธีการ:

  /// Contains all the data of this [DocumentSnapshot].
  Map<String, dynamic> data() {
    return _CodecUtility.replaceDelegatesWithValueInMap(
        _delegate.data(), _firestore);
  }

ที่ส่งคืน a Map<String, dynamic>

4 hayashi-ay Dec 15 2020 at 21:48

มันเป็นเพราะDocumentSnapshot.data ()Map<String, dynamic>เป็นฟังก์ชันที่ผลตอบแทน

ดังนั้นคำตอบคือ:

  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data(), reference: snapshot.reference);
1 Gene Dec 15 2020 at 04:26

เห็นได้ชัดว่าสิ่งที่ต้องการคือวงเล็บบนแผนที่

import 'package:cloud_firestore/cloud_firestore.dart';

class Record {
  final String name;
  final int votes;
  final DocumentReference reference;

  Record.fromMap(Map<String, dynamic> map(), {this.reference})
      : assert(map()['name'] != null),
        assert(map()['votes'] != null),
        name = map()['name'],
        votes = map()['votes'];

  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);

  @override
  String toString() => "Record<$name:$votes>";
}

KennedyOwusu Aug 26 2020 at 19:28

นี่คือวิธีที่ฉันแก้ไข

import 'package:cloud_firestore/cloud_firestore.dart';

class Record {
  final String name;
  final int totalVotes;
  final DocumentReference reference;

  Record.fromMap(Map<String, dynamic> map(), {this.reference})
      : assert(map()['name'] != null),
        assert(map()['totalVotes'] != null),
        name = map()['name'],
        totalVotes = map()['totalVotes'];

  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);

  @override
  String toString() => "Record<$name:$totalVotes>";
}

class Voters {
  String uid;
  String voteId;
  String markedVoteOption;
}


 /// Contains all the data of this [DocumentSnapshot].
  Map<String, dynamic> data() {
    return _CodecUtility.replaceDelegatesWithValueInMap(
        _delegate.data(), _firestore);
  }