【问题标题】:The argument type 'int' can't be assigned to the parameter type 'Expression<int, IntType>'参数类型“int”不能分配给参数类型“Expression<int, IntType>”
【发布时间】:2020-02-24 14:34:09
【问题描述】:

我正在使用 moor_flutter 包与 Flutter 应用程序上的 sqlite 数据库进行交互。我试图将明天解析为一个月中的天数。例如今天的日期是24,因此我在moor_flutter 的isSmallerOrEqual() 方法中将25 解析为明天。目的是将25解析为runtimetype **Expression&lt;int, IntType&gt;**,但我将其解析为runtimetype **int**,这是因为我不知道如何将int转换为Expression&lt;int, IntType&gt;。我尝试了一些不同的方法,但没有一个成功。

以下是我正在执行此操作的功能。

Future NearDueDate() {
    // final DateTime currentDate = new DateTime.now();
    var dayToday = currentDate.day;
    var tommorow = int.parse(dayToday.toString()) + 1;
    return (select(the_records)
          ..where((t_r) => t_r.due_date.day.isSmallerOrEqual(tommorow)))
        .get();
  }

注意这里的问题是如何将int 转换为Expression&lt;int, IntType&gt;,这样我就不会在t_r.due_date.day.isSmallerOrEqual(tommorow) 上收到任何错误?

谢谢你,用爱发帖。

【问题讨论】:

    标签: sqlite datetime flutter dart flutter-moor


    【解决方案1】:

    对不起,我不能评论这个答案。

    如果Expression 是某种模型。您必须通过创建Expression 的实例来转换它。

    以此为例:

    我们有一个模型,User

    class User {
    
        int _id;
        String _name;
    
        User(this._name);
    
        User.withId(this._id, this._name );
    
        int get id => _id;
    
        String get name => _name;
    
        set name(String newName) {
            if (newName.length <= 255) {
                this._name = newName;
            }
        }
    
        // Convert a User object into a Map object
        Map<String, dynamic> toMap() {
    
            var map = Map<String, dynamic>();
            if (id != null) {
                map['id'] = _id;
            }
            map['name'] = _name;
    
            return map;
        }
    
        // Extract a User object from a Map object
        User.fromMapObject(Map<String, dynamic> map) {
            this._id = map['id'];
            this._name = map['name'];
        }
    }
    

    所以这是这里的重点

    要创建User 的类型,我们会这样做

    user = User('John');
    

    现在user.name 的类型将是User&lt;String&gt;

    通常,'John' 只是一个字符串。现在它是 User 类的一部分。是User&lt;String&gt;

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2022-10-12
      相关资源
      最近更新 更多