Тип аргумента '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);
}
который возвращает 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);
}