رونمایی از پخته‌ترین و کاربردی‌ترین محصولات آموزشی سون‌لرن با کلی آفرهای ویژه🔥
۰ ثانیه
۰ دقیقه
۰ ساعت
۱ Mohammad Kahkeshani
Hive error:
جامعه فلاتر ایجاد شده در ۱۷ اردیبهشت ۱۴۰۲

سلام من به این مشکل برخوردم

Hive error:the box "Task" is already open and of type Box<

اینم کد هام

import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:to_do_list/data.dart';
const taskBoxName = 'tasks';
void main() async {
  await Hive.initFlutter();
  Hive.registerAdapter(TaskAdapter());
  await Hive.openBox(taskBoxName);
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const HomeScreen());
  }
}
class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});
  @override
  Widget build(BuildContext context) {
    final box = Hive.box(taskBoxName);
    return Scaffold(
      appBar: AppBar(title: const Text('To Do List')),
      floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            Navigator.of(context).pop(
                MaterialPageRoute(builder: (context) => EditingTaskScreen()));
          },
          label: Text('Add new Task')),
      body: ListView.builder(
          itemCount: box.values.length,
          itemBuilder: (context, index) {
            final Task task = box.values.toList()[index];
            return Container(
              child: Text(task.name),
            );
          }),
    );
  }
}
class EditingTaskScreen extends StatelessWidget {
  final TextEditingController _controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Edit Task'),
      ),
      floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            final task = Task();
            task.name = _controller.text;
            task.priority = Priority.low;
            task.save();
            Navigator.of(context).pop();
          },
          label: Text('save changes')),
      body: Column(
        children: [
          TextField(
            controller: _controller,
            decoration: InputDecoration(label: Text('Add a task for today...')),
          )
        ],
      ),
    );
  }
}

تایپ باکس (T) رو مشخص کنید فکر کنم اوکی بشه:

Hive.openBox<T>("box_name")
بهترین پاسخ
مهرداد فدایی ۱۸ اردیبهشت ۱۴۰۲، ۰۸:۴۳