Der Argumenttyp 'Map <String, dynamic> Function ()' kann dem Parametertyp 'Map <String, dynamic>' nicht zugeordnet werden.

Aug 26 2020

Dies konnte anfangs funktionieren, aber nach dem Firebase-Update wird mir jetzt dieser Fehler angezeigt. Ich habe dem Teil, der den Fehler verursacht, Sternchen hinzugefügt. Die Fehlermeldung wurde unter dem Code hinzugefügt.

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

Der Argumenttyp 'Map <String, dynamic> Function ()' kann dem Parametertyp 'Map <String, dynamic>' nicht zugeordnet werden.

Antworten

15 PeterHaddad Aug 26 2020 at 19:23

Versuche Folgendes:

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

data() ist eine Methode:

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

das gibt a zurück Map<String, dynamic>

4 hayashi-ay Dec 15 2020 at 21:48

Dies liegt daran, dass DocumentSnapshot.data () eine Funktion ist, die zurückgibt Map<String, dynamic>.

Die Antwort lautet also:

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

Anscheinend waren nur Klammern auf den Karten erforderlich

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

So habe ich es gelöst.

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