1

I am wanting to write user input into a box to store it.

I am getting the error The box "user_api" is already open and of type Box<String>.

I only opened it in the main() function and then closed it in the _API_Page_State

I am confused how I keep running into this problem. Can anyone help? Thanks.

(Input anything into the TextInput bar.)

My Code:

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
            );
          })
        ],
          )
        ),
    );}}



Jake B.
  • 99
  • 5

3 Answers3

2

I have the same problem and the solution proposed by Ray Zion works perfectly for me. The solution is the same as Jake B. commented above.

If you opened a hive box using generic await Hive.openBox<String>('user_api'), you need to use it later using generic Hive.box<String>('user_api').

gilbriatore
  • 658
  • 7
  • 12
1

You can refer to the Hive specs: https://docs.hivedb.dev/#/basics/boxes?id=type-parameter-boxltegt

When calling openBox:

...It is important that you provide the same type parameter to Hive.box(). You cannot open the same box multiple times with different type parameters.

Will59
  • 1,430
  • 1
  • 16
  • 37
0

I found that I had to insert <String> inside of var box = Hive.box<String>('user_api');

So the fix was: var box = Hive.box<String>('user_api');

Jake B.
  • 99
  • 5