Typ argumentu „Map <String, dynamic> Function ()” nie może być przypisany do typu parametru „Map <String, dynamic>”

Aug 26 2020

To mogło początkowo działać, ale po aktualizacji Firebase teraz daje mi ten błąd. Dodałem gwiazdki do części podającej błąd. Komunikat o błędzie został dodany pod kodem.

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

Typ argumentu „Map <String, dynamic> Function ()” nie może być przypisany do typu parametru „Map <String, dynamic>”.

Odpowiedzi

15 PeterHaddad Aug 26 2020 at 19:23

Spróbuj wykonać następujące czynności:

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

data() to metoda:

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

która zwraca plik Map<String, dynamic>

4 hayashi-ay Dec 15 2020 at 21:48

Dzieje się tak, ponieważ DocumentSnapshot.data () jest funkcją, która zwraca Map<String, dynamic>.

Tak więc odpowiedź brzmi:

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

Najwyraźniej wszystko, co było potrzebne, to nawiasy na mapach

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

Tak to rozwiązałem.

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