flutter 错误:参数类型“Object Function()”无法分配给参数类型“Map<String, dynamic>”

问题描述 投票:0回答:2

我尝试在flutter材质应用中为玩家创建一个模型。我创建了一个名为“player”的类并实现它。我编辑 gradle 文件以与 firebase 连接。我的代码中出现了一些错误。所以请帮我解决这个错误。

错误

Error: The argument type 'Object Function()' can't be assigned to the parameter type 'Map<String, dynamic>'.
 - 'Object' is from 'dart:core'.
 - 'Map' is from 'dart:core'.
      : this.fromMap(snapshot.data, documentReference: snapshot.reference);
                              ^


FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1035

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 11m 3s
Exception: Gradle task assembleDebug failed with exit code 1

这是模型类。这里我添加了一些玩家的属性。

模型类

import 'package:cloud_firestore/cloud_firestore.dart';

class player {
  String bio;
  String name;
  String city;
  String age;
  String role;
  String runs;
  String avg;
  String wiki;

  DocumentReference documentReference;

  player(
      {this.bio,
      this.name,
      this.city,
      this.age,
      this.role,
      this.runs,
      this.avg,
      this.wiki});
  player.fromMap(Map<String, dynamic> map, {this.documentReference}) {
    bio = map["bio"];
    name = map["name"];
    city = map["city"];
    age = map["age"];
    role = map["role"];
    runs = map["runs"];
    avg = map["avg"];
    wiki = map["wiki"];
  }
  player.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, documentReference: snapshot.reference);

  toJson() {
    return {
      'bio': bio,
      'name': name,
      'city': city,
      'age': age,
      'role': role,
      'runs': runs,
      'avg': avg,
      'wiki': wiki
    };
  }
}

flutter entity-framework flutter-dependencies
2个回答
0
投票

使用

snapshot.data()
而不是
snapshot.data
迁移指南中提到了。

突破:现在可以通过 data() 方法通过数据获取器获取快照数据。


0
投票
like this:

player.fromSnapshot(DocumentSnapshot snapshot)
    : this.fromMap(snapshot.data() as Map<String, dynamic>, documentReference: snapshot.reference);


Full Updated Model Class:


import 'package:cloud_firestore/cloud_firestore.dart';

class Player {
  String bio;
  String name;
  String city;
  String age;
  String role;
  String runs;
  String avg;
  String wiki;

  DocumentReference documentReference;

  Player({
    this.bio,
    this.name,
    this.city,
    this.age,
    this.role,
    this.runs,
    this.avg,
    this.wiki,
  });

  Player.fromMap(Map<String, dynamic> map, {this.documentReference}) {
    bio = map["bio"];
    name = map["name"];
    city = map["city"];
    age = map["age"];
    role = map["role"];
    runs = map["runs"];
    avg = map["avg"];
    wiki = map["wiki"];
  }

  Player.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data() as Map<String, dynamic>, documentReference: snapshot.reference);

  Map<String, dynamic> toJson() {
    return {
      'bio': bio,
      'name': name,
      'city': city,
      'age': age,
      'role': role,
      'runs': runs,
      'avg': avg,
      'wiki': wiki,
    };
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.