我正在按照教程使用 hive 缓存 api 响应。
下面是其中包含 part 'posts_model.g.dart'; 的代码,但是构建运行程序向我提供了此消息,而不是创建文件。
[警告] lib/posts_model_dart.dart 上的 source_gen:combining_builder: posts_model_dart.g.dart 必须作为部分指令包含在 输入库: 'posts_model_dart.g.dart' 部分;
它似乎在告诉我,我需要这个部分'posts_model.g.dart';,我有
import 'dart:convert';
import 'package:hive_flutter/hive_flutter.dart';
part 'posts_model.g.dart';
@HiveType(typeId: 0)
class PostsModel {
@HiveField(0)
final int? userId;
@HiveField(1)
final int? id;
@HiveField(2)
final String? title;
@HiveField(3)
final String? body;
PostsModel({
this.userId,
this.id,
this.title,
this.body,
});
Map<String, dynamic> toMap() {
return <String, dynamic>{
'userId': userId,
'id': id,
'title': title,
'body': body,
};
}
factory PostsModel.fromMap(Map<String, dynamic> map) {
return PostsModel(
userId: map['userId'] != null ? map['userId'] as int : null,
id: map['id'] != null ? map['id'] as int : null,
title: map['title'] != null ? map['title'] as String : null,
body: map['body'] != null ? map['body'] as String : null,
);
}
String toJson() => json.encode(toMap());
factory PostsModel.fromJson(String source) =>
PostsModel.fromMap(json.decode(source) as Map<String, dynamic>);
}
您的文件名是
posts_model_dart.dart
,而不是 posts_model.dart
。所以零件线应该是:
part 'posts_model_dart.g.dart';
或者,您可能想将文件实际重命名为
posts_model.dart
,因为这看起来多余:)