hiveerror:框“user”已打开,类型为box< user>

ygya80vv  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(445)

我试着用 Hive 内部Flutter Mobx ,在签入用户数据后 Hive 我切换到另一个屏幕,如 HomeView 或者
Intro main.dart :

  1. Future<void> main() async {
  2. ...
  3. final appDocumentDirectory = await path_provider.getApplicationDocumentsDirectory();
  4. Hive.init(appDocumentDirectory.path);
  5. Hive.registerAdapter(UserAdapter());
  6. _setUpLogging();
  7. runApp(MultiProvider(providers: providers, child: StartupApplication()));
  8. }
  9. ``` `StartupApplication` 班级:我不使用 `Hive` ```
  10. class StartupApplication extends StatelessWidget {
  11. @override
  12. Widget build(BuildContext context) {
  13. final isPlatformDark = WidgetsBinding.instance.window.platformBrightness == Brightness.dark;
  14. final initTheme = isPlatformDark ? nebrassLightTheme : nebrassLightTheme;
  15. return ThemeProvider(
  16. initTheme: initTheme,
  17. duration: const Duration(milliseconds: 400),
  18. child: Builder(builder: (context) {
  19. return MaterialApp(
  20. title: 'TEST',
  21. theme: ThemeProvider.of(context),
  22. home: const OverlaySupport(child: OKToast(
  23. child: MyHomePage() //--> checking user data widget
  24. )),
  25. onGenerateRoute: Routes.sailor.generator(),
  26. navigatorKey: Routes.sailor.navigatorKey,
  27. );
  28. }),
  29. );
  30. }
  31. }

检查 UserHive 内部 MyHomePage 班级:

  1. class MyHomePage extends StatefulWidget {
  2. const MyHomePage({Key key}) : super(key: key);
  3. @override
  4. _MyHomePageState createState() => _MyHomePageState();
  5. }
  6. class _MyHomePageState extends State<MyHomePage> {
  7. @override
  8. Widget build(BuildContext context) {
  9. return FutureBuilder<Box<User>>(
  10. future: Hive.openBox('user'),
  11. builder: (context, snapshot) {
  12. if (snapshot.connectionState == ConnectionState.done) {
  13. final Box<User> userBox = snapshot.data;
  14. if (userBox.values.isNotEmpty && userBox.get(0).active == 1) {
  15. return HomeView();
  16. } else {
  17. return Intro();
  18. }
  19. } else {
  20. return Container();
  21. }
  22. });
  23. }
  24. @override
  25. void dispose() {
  26. Hive.close();
  27. super.dispose();
  28. }
  29. }

现在在其他屏幕中,例如 RegisterScreen 一级实施 MobX 我想在里面 user 框,例如:

  1. class Register extends StatefulWidget {
  2. @override
  3. _RegisterState createState() => _RegisterState();
  4. }
  5. class _RegisterState extends State<Register> {
  6. TextEditingController _mobileNumber;
  7. final GlobalKey<ScaffoldState> _scaffoldState = GlobalKey<ScaffoldState>();
  8. @override
  9. void initState() {
  10. super.initState();
  11. _mobileNumber = TextEditingController();
  12. }
  13. @override
  14. Widget build(BuildContext context) {
  15. final _registerViewModel = Provider.of<RegisterViewModel>(context, listen: false);
  16. return Directionality(
  17. textDirection: TextDirection.ltr,
  18. child: Scaffold(
  19. key: _scaffoldState,
  20. ...
  21. //_registerViewModel.registerAccount(_mobileNumber.text, '111');
  22. ),
  23. );
  24. }
  25. void _showSnackBar(String message, BuildContext context) {
  26. _scaffoldState.currentState.showSnackBar(SnackBar(
  27. content: Directionality(
  28. textDirection: TextDirection.rtl,
  29. child: Text(
  30. '$message',
  31. style: AppTheme.of(context).caption().copyWith(color: Colors.white),
  32. ))));
  33. }
  34. }
  35. ``` `MobX` 实施:

enum RegisterLoadingState { loading, done }
enum ActiveLoadingState { loading, done }
enum RegisteringState { initial, registered, activated, registerError, activeError }

class RegisterViewModel = _RegisterViewModel with _$RegisterViewModel;

abstract class _RegisterViewModel with Store {
final WebApi _webApi;

_RegisterViewModel({@required WebApi webApi}) : _webApi = webApi;

...

@action
Future registerAccount(String mobileNumber, String deviceId) async {
final RegisterRequest _request = RegisterRequest(mobileNumber, deviceId);
try {
loadingState = RegisterLoadingState.loading;
final _res = await _webApi.register(_request);
loadingState = RegisterLoadingState.done;

  1. _registerResponse = RegisterResponse.fromJson(_res.body as Map<String, dynamic>);
  2. /* I GET ERROR IN THIS LINE -- HiveError: The box "user" is already open and of type Box<User>.*/
  3. final userBox = await Hive.openBox('user');
  4. final user = User(/*...*/);
  5. userBox.putAt(0, user);

}

@action
Future activeAccount(String mobileNumber, String verifyCode) async {
final ActiveAccountRequest _activeAccount = ActiveAccountRequest(mobileNumber, verifyCode);

  1. final userBox = await Hive.openBox('user');
  2. final User currentUser = userBox.getAt(0) as User;
  3. final user = User(/*...*/);
  4. userBox.putAt(0, user);

}
}

zu0ti5jz

zu0ti5jz1#

你可以打开你的 user 盒子里的 main 应用程序的方法:

  1. Future<void> main() async {
  2. ...
  3. final appDocumentDirectory = await path_provider.getApplicationDocumentsDirectory();
  4. Hive.init(appDocumentDirectory.path);
  5. Hive.registerAdapter(UserAdapter());
  6. // open the user box
  7. await Hive.openBox('user');
  8. _setUpLogging();
  9. runApp(MultiProvider(providers: providers, child: StartupApplication()));
  10. }

访问之前打开的框,如下所示:

  1. class MyHomePage extends StatefulWidget {
  2. const MyHomePage({Key key}) : super(key: key);
  3. @override
  4. _MyHomePageState createState() => _MyHomePageState();
  5. }
  6. class _MyHomePageState extends State<MyHomePage> {
  7. // user box
  8. Box userBox;
  9. @override
  10. void initState() {
  11. super.initState();
  12. // get the previously opened user box
  13. userBox = Hive.box('user');
  14. }
  15. @override
  16. Widget build(BuildContext context) {
  17. // check for your conditions
  18. return (userBox.values.isNotEmpty && userBox.get(0).active == 1)
  19. ? HomeView()
  20. : Intro();
  21. }
  22. }
展开查看全部

相关问题