【问题标题】:Flutter HiveDB Using box on FutureBuilderFlutter HiveDB 在 FutureBuilder 上使用框
【发布时间】:2020-06-27 20:58:58
【问题描述】:

我是 Flutter 中 Hive 数据库的新手,我想弄清楚为什么我不能在 FutureBuilder 中使用 Hive.openBox ?

FutureBuilder(
  future: Hive.openBox<User>('userBox'),
  builder: (context, snapshot) {
    if (snapshot.connectionState == ConnectionState.done) {
      final User userAccount = snapshot.data;
      if (userAccount == null) {
        return Center(
            child: Container(
          child: Text('LOGIN'),
        ));
      } else {
        return Center(
            child: Container(
          child: Text('SUCCESS'),
        ));
      }
    }
    return Container(
      width: 0.0,
      height: 0.0,
    );
  },
);

我得到这个错误:

type 'BoxImpl' is not a subtype of type 'User'

但是这段代码在main函数中工作正常:

var box =  await Hive.openBox<User>('userBox');

User类:

@HiveType(typeId: 1)
class User{
  @HiveField(0)
  String username;
  @HiveField(1)
  String password;
  @HiveField(2)
  String mobileNumber;
  @HiveField(3)
  String biography;
  @HiveField(4)
  String avatar;
  @HiveField(5)
  String website;
  @HiveField(6)
  String linkedinLink;
  @HiveField(7)
  String githubLink;
  @HiveField(8)
  String facebookLink;
  @HiveField(9)
  String telegramLink;
  @HiveField(10)
  String twitterLink;
  @HiveField(11)
  PageInfo pageInfo;

  User(this.username, this.password, this.mobileNumber, this.biography, this.avatar, this.website, this.linkedinLink, this.githubLink,
      this.facebookLink, this.telegramLink, this.twitterLink, this.pageInfo);
}

main.dart

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  var appDocumentDirectory = await path_provider.getApplicationDocumentsDirectory();

  Hive
    ..init(appDocumentDirectory.path)
    ..registerAdapter(UserAdapter())
    ..registerAdapter(PageInfoAdapter());

  runApp(MultiProvider(providers: providers, child: OKToast(child: StartupApplication())));
}

【问题讨论】:

    标签: flutter dart hivedb


    【解决方案1】:

    问题已解决,即在 FutureBuilder 内投射用户

    builder: (context, AsyncSnapshot<Box<User>> snapshot) {
      if (snapshot.connectionState == ConnectionState.done) {
        final Box<User> userAccount = snapshot.data;
        if (userAccount.isEmpty) {
          return Center(
              child: Container(
                child: Text('LOGIN',style: TextStyle(color: Colors.white),),
              ));
        } else {
          return Center(
              child: Container(
                child: Text('SUCCESS',style: TextStyle(color: Colors.white),),
              ));
        }
      }
      return Container(
        width: 0.0,
        height: 0.0,
      );
    },
    

    【讨论】:

      【解决方案2】:

      替换这个

      ..init(appDocumentDirectory.path)

      与

      ..initFlutter(appDocumentDirectory.path);

      【讨论】:

      • HiveInterface 没有initFlutter
      • 对非 Flutter 应用使用 Hive.init()。
      • 我认为我的代码问题出在这一行final User userAccount = snapshot.data;
      • 我不认为它根据您的代码清楚地返回用户。我希望你能正确生成文件
      猜你喜欢
      • 1970-01-01
      • 2021-11-22
      • 2021-01-16
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 2021-05-04
      • 2018-11-23
      • 1970-01-01
      相关资源
      最近更新 更多