Flutter Realm 修改数据模型后报错解决方法

发布于 1周前 作者 phonegap100 最后一次编辑是 5天前 来自 分享

Flutter Realm 修改数据模型后报错怎么办?

PropertyCarmake' has been removed.

Property 'Car.kilometers' has been removed.

Property 'Car.brand' has been added.

Property "Person.age' has been made optional.

Property "Person.sex' has been added.

如下:

以前的数据模型

import 'package:realm/realm.dart';
part 'main.g.dart';
@RealmModel()
class _Person {
  late String name;  
  int age = 1;
}

后面改成了

import 'package:realm/realm.dart';
part 'main.g.dart';
@RealmModel()
class _Person {
  late String name;  
  late String sex;  
  int age = 1;
}

这个时候运行可能提示 Property "Person.sex’ has been added.

如下图:

1680596188167.png

解决办法:配置schemaVersion

参考:https://www.mongodb.com/docs/realm/sdk/flutter/realm-database/model-data/update-realm-object-schema/#automatically-update-schema

void initState() {    
    final config = Configuration.local([Car.schema,Person.schema],schemaVersion: 2);
    realm = Realm(config);    
    super.initState();
  }
回到顶部