【问题标题】:The argument type 'Map<String, dynamic> Function()' can't be assigned to the parameter type 'Map<String, dynamic>'参数类型“Map<String, dynamic> Function()”不能分配给参数类型“Map<String, dynamic>”
【发布时间】:2020-12-15 05:38:24
【问题描述】:

这最初可以工作,但在 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 Function()”不能分配给参数类型“Map”。

【问题讨论】:

  • 你从哪里得到这个错误?
  • 我在哪里添加了星号。 *** 就是这个。 Record.fromSnapshot(DocumentSnapshot 快照) : this.fromMap(snapshot.data, 参考:snapshot.reference);

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

尝试以下方法:

  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&lt;String, dynamic&gt;

【讨论】:

  • 根据您给出的答案,我能够操纵它并找到解决方法。你可以在下面查看我的答案。 @彼得哈达德
  • @KobbyDiscount 不,不要使用我应该使用的第二个代码 sn-p。我只是在这里向您展示 data() 方法 github.com/FirebaseExtended/flutterfire/blob/master/packages/… 的代码
  • @KobbyDiscount 你所要做的就是这个Record.fromSnapshot(DocumentSnapshot snapshot) : this.fromMap(snapshot.data(), reference: snapshot.reference);
  • 嗨,在此处和其他页面上尝试了所有解决方案后,我仍然遇到同样的错误
【解决方案2】:

这是因为DocumentSnapshot.data()是一个返回Map&lt;String, dynamic&gt;的函数。

所以,答案是:

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

【讨论】:

    【解决方案3】:

    显然需要的只是地图上的括号

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

    【讨论】:

    • 在尝试了所有提到的解决方案后,我仍然遇到同样的错误
    【解决方案4】:

    我就是这样解决的。

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

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 2021-08-29
      • 2021-10-02
      • 2020-12-27
      • 1970-01-01
      • 2021-05-17
      • 2021-10-24
      • 2023-04-05
      相关资源
      最近更新 更多