11

I have a Wallpaper App and it uses Firestore to store the wallpapers.

I want to use Hive to store a list of wallpapers from cloud firestore but how to save the List of Wallpapers and retrieve it later?

When I try to save the list I get this error:

E/flutter ( 9995): [ERROR:flutter/shell/common/shell.cc(199)] Dart Error: Unhandled exception: E/flutter ( 9995): HiveError: Cannot write, unknown type: Wallpaper. Did you forget to register an adapter?

Code:

class Wallpaper extends HiveObject {


  String date;
  String url;

  Wallpaper();

}

static Future<void> addWallpapers({@required String boxName, @required List<Wallpaper> wallpapers}) async {

    var box = await Hive.openBox(boxName);
    box.put(boxName, wallpapers);

    print("WALLPAPER ADICIONADO NO HIVE!");

  }

  static Future<List<Wallpaper>> getWallpapers({@required String boxName}) async {

    var box = await Hive.openBox(boxName);

    List<Wallpaper> wallpapers = box.get("latest");

    return wallpapers;

  }

How should I determine the lowest runable vscode version on my extension

The question is quite straightforward, how should I set the minimum required VSCode version on my extension? Should I add the version I'm working on? That doesn't sound optimal. Should I generate a random number? Should I collect all the APIs and manually check their version requirements? Any better idea?

4

2 回答 2

5

您必须使用 @HiveType() 注释您的对象。并且必须注册您的对象 Hive.registerAdapter(WallpaperAdapter(), 0);。

然而,您是否必须part 'wallpaper.g.dart';生成所需的代码?

编辑:首先在您的 pubspec 上导入依赖项:

dependencies:
  hive: ^[version]
  hive_flutter: ^[version]

dev_dependencies:
  hive_generator: ^[version]
  build_runner: ^[version]

Hive.registerAdapter(MyObjectAdapter(), 0);你应该把你的功能main.dart。在 runApp 之前

你的 HiveObject 应该有这样的注释:

@HiveType()
class Person extends HiveObject {
  @HiveField(0);
  String name;

  @HiveField(1);
  int age;
}

将此命令放在您的导入附近part 'person.g.dart';并在您的终端上运行代码生成。flutter packages pub run build_runner build.

带有代码生成的 Hive 功能,因此此命令将生成您需要的文件

于 2019-12-19T17:36:16.307 回答
3

我通过在 HiveType 上包含一个实际 ID 解决了这个问题。像这样:

  @HiveType(typeId: 0)
  class SoundSingle {

    @HiveField(0)
    final String name;

    @HiveField(1)
    final String fileName;

    @HiveField(2)
    int volume;

    SoundSingle(this.name,this.fileName, this.volume);
}

更多 HiveType 模型需要增加数字。所以每个值都是唯一的(我猜是连续的,但我没有对此进行测试)。

于 2020-07-07T13:34:38.597 回答