Il tipo di argomento 'Map<String, dynamic> Function()' non può essere assegnato al tipo di parametro 'Map<String, dynamic>'

Aug 26 2020

Inizialmente potrebbe funzionare, ma dopo l'aggiornamento di Firebase, ora mi sta dando questo errore. Ho aggiunto degli asterischi alla parte che riporta l'errore. Il messaggio di errore è stato aggiunto sotto il codice.

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

Il tipo di argomento 'Map<String, dynamic> Function()' non può essere assegnato al tipo di parametro 'Map<String, dynamic>'.

Risposte

15 PeterHaddad Aug 26 2020 at 19:23

Prova quanto segue:

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

data()è un metodo:

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

che restituisce aMap<String, dynamic>

4 hayashi-ay Dec 15 2020 at 21:48

È perché DocumentSnapshot.data() è una funzione che restituisce Map<String, dynamic>.

Quindi, la risposta è:

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

Apparentemente tutto ciò che serviva erano le parentesi sulle mappe

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

Ecco come l'ho risolto.

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