我的Flutter应用程序中的Map对象给我一个空函数错误

ckx4rj1h  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(120)

下面是代码给我的错误和错误是“一个函数体必须提供。尝试添加一个函数体。”我已经尝试了两种方法,但仍然有相同的错误。
下面是代码

Map<String, dynamic> toJson() {
return {"uid": uid, "email": email, "name": name,};}

字符串
我也试过这个代码

Map<String, dynamic> toJson() =>
  {"uid": uid, "email": email, "name": name,};


下面是主代码

import 'package:cloud_firestore/cloud_firestore.dart';

class UserModel {
  final String uid;
  final String name;
  final String email;

  UserModel({
    required this.uid,
    required this.name,
    required this.email,
  })

  Map<String, dynamic> toJson() =>
      {"uid": uid, "email": email, "name": name,};

  static UserModel fromSnap(DocumentSnapshot snap) {
    var snapshot = snap.data() as Map<String, dynamic>;

    return UserModel(
        uid: snapshot["uid"],
      name: snapshot["name"],
        email: snapshot["email"],
    );
  }
}


我该怎麽办?

ih99xse1

ih99xse11#

您忘记在UserModel构造函数后加上分号;

class UserModel {
  UserModel({
    required this.uid,
    required this.name,
    required this.email,
  });
  final String uid;
  final String name;
  final String email;

  Map<String, dynamic> toJson() => {
        'uid': uid,
        'email': email,
        'name': name,
      };

  static UserModel fromSnap(DocumentSnapshot snap) {
    final snapshot = snap.data() as Map<String, dynamic>;
    return UserModel(
      uid: snapshot['uid'] as String,
      name: snapshot['name'] as String,
      email: snapshot['email'] as String,
    );
  }
}

字符串

相关问题