build_runner 未创建文件

问题描述 投票:0回答:1

我正在按照教程使用 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>);
}
flutter hive
1个回答
0
投票

您的文件名是

posts_model_dart.dart
,而不是
posts_model.dart
。所以零件线应该是:

part 'posts_model_dart.g.dart';

或者,您可能想将文件实际重命名为

posts_model.dart
,因为这看起来多余:)

© www.soinside.com 2019 - 2024. All rights reserved.