Flutter -SEMBAST 与 built_value 的集成,如何将模型插入 sembast 数据库
Flutter -integration of SEMBAST with built_value , how to insert the Model into the sembast db
我尝试使用将 Post 对象插入到 SEMBAST 数据库中,但是需要 toMap() 才能插入到数据库中
请在下面找到我的代码
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import '../serializers.dart';
import 'articles.dart';
part 'news.g.dart';
abstract class News implements Built<News, NewsBuilder> {
News._();
factory News([updates(NewsBuilder b)]) = _$News;
@BuiltValueField(wireName: 'status')
String get status;
@BuiltValueField(wireName: 'totalResults')
int get totalResults;
@BuiltValueField(wireName: 'articles')
BuiltList<Articles> get articles;
String toJson() {
return json.encode(serializers.serializeWith(News.serializer, this));
}
static News fromJson(String jsonString) {
return serializers.deserializeWith(
News.serializer, json.decode(jsonString));
}
static Serializer<News> get serializer => _$newsSerializer;
}
我的数据源文件是
import 'package:boilerplate/data/local/constants/db_constants.dart';
import 'package:boilerplate/models/news/news.dart';
import 'package:boilerplate/models/post/post.dart';
import 'package:boilerplate/models/post/post_list.dart';
import 'package:sembast/sembast.dart';
class NewsDataSource {
// A Store with int keys and Map<String, dynamic> values.
final _postsStore = intMapStoreFactory.store(DBConstants.STORE_NAME);
// Private getter to shorten the amount of code needed to get the
// singleton instance of an opened database.
// Future<Database> get _db async => await AppDatabase.instance.database;
// database instance
final Future<Database> _db;
// Constructor
PostDataSource(this._db);
// DB functions:--------------------------------------------------------------
Future insert(News news) async {
return await _postsStore.add(await _db, post);
}
}
但是插入语法 await _postsStore.add(await _db, post); 显示错误
Future<int> StoreRef.add(DatabaseClient client, Map<String, dynamic> value)
请帮我解决一下
如前所述,sembast 与 firestore 和 indexed_db 一样期待地图。您必须以兼容的方式使用 built_value 进行序列化。一种方法是在定义序列化程序时使用 StandardJsonPlugin:
@SerializersFor([News])
final Serializers serializers =
(_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
然后您可以从新闻对象生成地图:
var map = serializers.serializeWith(News.serializer, news);
你的插入方法将是这样的:
Future insert(News news) async {
var map = serializers.serializeWith(News.serializer, news);
return await _postsStore.add(await _db, map);
}
我尝试使用将 Post 对象插入到 SEMBAST 数据库中,但是需要 toMap() 才能插入到数据库中 请在下面找到我的代码
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import '../serializers.dart';
import 'articles.dart';
part 'news.g.dart';
abstract class News implements Built<News, NewsBuilder> {
News._();
factory News([updates(NewsBuilder b)]) = _$News;
@BuiltValueField(wireName: 'status')
String get status;
@BuiltValueField(wireName: 'totalResults')
int get totalResults;
@BuiltValueField(wireName: 'articles')
BuiltList<Articles> get articles;
String toJson() {
return json.encode(serializers.serializeWith(News.serializer, this));
}
static News fromJson(String jsonString) {
return serializers.deserializeWith(
News.serializer, json.decode(jsonString));
}
static Serializer<News> get serializer => _$newsSerializer;
}
我的数据源文件是
import 'package:boilerplate/data/local/constants/db_constants.dart';
import 'package:boilerplate/models/news/news.dart';
import 'package:boilerplate/models/post/post.dart';
import 'package:boilerplate/models/post/post_list.dart';
import 'package:sembast/sembast.dart';
class NewsDataSource {
// A Store with int keys and Map<String, dynamic> values.
final _postsStore = intMapStoreFactory.store(DBConstants.STORE_NAME);
// Private getter to shorten the amount of code needed to get the
// singleton instance of an opened database.
// Future<Database> get _db async => await AppDatabase.instance.database;
// database instance
final Future<Database> _db;
// Constructor
PostDataSource(this._db);
// DB functions:--------------------------------------------------------------
Future insert(News news) async {
return await _postsStore.add(await _db, post);
}
}
但是插入语法 await _postsStore.add(await _db, post); 显示错误
Future<int> StoreRef.add(DatabaseClient client, Map<String, dynamic> value)
请帮我解决一下
如前所述,sembast 与 firestore 和 indexed_db 一样期待地图。您必须以兼容的方式使用 built_value 进行序列化。一种方法是在定义序列化程序时使用 StandardJsonPlugin:
@SerializersFor([News])
final Serializers serializers =
(_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();
然后您可以从新闻对象生成地图:
var map = serializers.serializeWith(News.serializer, news);
你的插入方法将是这样的:
Future insert(News news) async {
var map = serializers.serializeWith(News.serializer, news);
return await _postsStore.add(await _db, map);
}