dart - 使用 getter 和 setter 从 1-7 个整数创建字符串

标签 dart flutter

我正在尝试取一个 1-7 整数值并使用枚举为每个值打印出一天。

我在 firestore 的类映射中遇到错误,“不是封闭类中的字段”

因此,如果传入 1,则给出“星期一” 如果传入 2,则给出“星期二”

enum _Days {
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday
}

class HeadingItem implements ListItem {
  String _weekday;
  final int time;
  final DocumentReference reference;

  set day(int weekday) {
    var value = _Days.values[weekday - 1].toString();
    var idx = value.indexOf(".") + 1;
    var result = value.substring(idx, value.length);
    _weekday = result;
  }

  String get day {
    return _weekday;
  }

  HeadingItem.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map['day'] != null),
        assert(map['time'] != null),
        day = map['day'], // 'day' isn't a field in the enclosing class  <--- this is the error that im stuck on...
        time = map['time'];

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

最佳答案

改变

String get day {
  return _weekday;
}

为此

String day = _weekday;

关于dart - 使用 getter 和 setter 从 1-7 个整数创建字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53124685/

相关文章:

具有相同 key.jks 的 Flutter 签名应用程序无法正常工作

flutter GetX : Animation with GetxController

Flutter - 使用 Linkedin API 和 linkedin_login 包获取配置文件 URL

json - Flutter从互联网上获取数据

flutter : Image on ListTile leading too small

flutter - 关闭抽屉后重新聚焦 TextField

flutter - 在 flutter 中将 List.toString() 转换为 List

flutter - 如何在firestore flutter中上传自定义数据模型

android - Flutter app后台使用compute函数

dart - 最新的稳定版本(v1.1.1)-Dartium上不提供内置库 'dart:io'