【问题标题】:Flutter - HiveError: Box is Already OpenFlutter - HiveError:盒子已经打开
【发布时间】:2022-07-21 21:51:00
【问题描述】:

我想将用户输入写入一个盒子来存储它。

我收到错误 The box "user_api" is already open and of type Box<String>.

我只在main()函数中打开它,然后在_API_Page_State函数中关闭它

我很困惑我是如何一直遇到这个问题的。任何人都可以帮忙吗?谢谢。

(在 TextInput 栏中输入任何内容。)

我的代码:

import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:async';
import 'dart:io';
import 'package:fluttertoast/fluttertoast.dart';



Future<void> main() async {

  // HIVE SETUP---------------------------------------------
  WidgetsFlutterBinding.ensureInitialized();

  Directory directory = await getApplicationDocumentsDirectory();
  Hive.init(directory.path);
  await Hive.openBox<String>('user_api'); // Initially Opens Box on App Start

  await Hive.initFlutter();

  // HIVE SETUP---------------------------------------------                      *LATER: Set up Encrypted Box for the API Key. Set Up Unencrypted Box for other information.*

  runApp(API_Page_());
}



class API_Page_ extends StatefulWidget {
  const API_Page_({Key? key}) : super(key: key);

  @override
  _API_Page_State createState() => _API_Page_State();}

class _API_Page_State extends State<API_Page_> {

  @override
  void dispose() {
    Hive.box('user_api').close();
    super.dispose();
  }

  final TextEditingController _apiKeyController = TextEditingController();


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Indietools Login'), backgroundColor: Color(0xFF7E57C2),),
        body: Center(child: Column(children: <Widget>[
          TextField(
            decoration: InputDecoration(
              label: Text('API Key'),              
            ),
            controller: _apiKeyController,
          ),
          RaisedButton(
            onPressed: () {
              Text('Store in Box');
              final api_key_input = _apiKeyController.text;
              var box = Hive.box('user_api');
              box.put('API: ', api_key_input);
            },
          ),
          RaisedButton(onPressed: () {
            Text('Retrieve API in Box');
            var box = Hive.box('user_api');
            String data = box.get('API');
            Fluttertoast.showToast(
              msg: data,
              toastLength: Toast.LENGTH_LONG,
              backgroundColor: Colors.red,
              textColor: Colors.white,
              fontSize: 16.0
            );
          })
        ],
          )
        ),
    );}}



【问题讨论】:

  • 尝试将await Hive.initFlutter(); 移动到await Hive.openBox&lt;String&gt;('user_api'); 上方。

标签: flutter textinput box flutter-hive


【解决方案1】:

我发现我必须在var box = Hive.box&lt;String&gt;('user_api'); 中插入&lt;String&gt;

所以修复是:var box = Hive.box&lt;String&gt;('user_api');

【讨论】:

    【解决方案2】:

    我也有同样的问题,Ray Zion 提出的solution 非常适合我。解决方案与上面评论的 Jake B. 相同。

    如果你使用泛型await Hive.openBox('user_api')打开了一个蜂巢盒,你需要稍后使用泛型Hive.box('user_api')来使用它>.

    【讨论】:

      猜你喜欢
      • 2020-12-18
      • 2021-03-04
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 2022-11-19
      • 2020-03-13
      相关资源
      最近更新 更多