0

我在我的应用程序中使用 hive DB,当我第一次运行我的代码时一切都很好(我可以添加主类别我可以添加子类别),但是在关闭并打开我的应用程序之后,我可以看到我在关闭之前保存的所有类应用程序,但是当我在重新打开应用程序后尝试添加新类别时,该应用程序会在我添加的新类别中显示第一个类别的项目。为什么我的应用程序在我关闭并再次打开时出现问题。谢谢你的帮助

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: SafeArea(
        child: Scaffold(
          backgroundColor: kColorTheme1,
          appBar: AppBar(
            centerTitle: true,
            automaticallyImplyLeading: false,
            elevation: 20,
            backgroundColor: Color(0xFFF2C3D4).withOpacity(1),
            title:TitleBorderedText(title:"Sevimli Yemekler", textColor: Color(0xFFFFFB00)),
            actions: [
              CircleAvatar(
                radius: 27,
                backgroundColor: Colors.transparent,
                backgroundImage: AssetImage(kCuttedLogoPath),
              ),
            ],
          ),
          body: Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(kBGWithLogoOpacity),
                    fit: BoxFit.cover,
                  ),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    Expanded(
                      child:GridView.builder (
                          scrollDirection: Axis.vertical,
                          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
                          itemCount: _categoryModelsBox.length+1,
                          itemBuilder: (context,index) {
                            if(_categoryModelsBox.length==0){
                              return EmptyCard(where: "homeScreen",);
                            }
                            if(_flag==1){
                              return EmptyCard(where: "homeScreen",);
                            }
                            if(index==_categoryModelsBox.length-1){
                              _flag=1;
                              CategoryModel categoryModel = _categoryModelsBox.getAt(index);
                              return CategoryCard(category: categoryModel);
                            }
                            else{
                              CategoryModel categoryModel = _categoryModelsBox.getAt(index);
                              return CategoryCard(category: categoryModel);
                            }
                          }
                      ),
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        Padding(
                          padding: EdgeInsets.all(10),
                          child: Container(
                            decoration: BoxDecoration(
                              border: Border.all(style: BorderStyle.solid),
                              color: kColorTheme7,
                              borderRadius: BorderRadius.circular(40),
                            ),
                            child: TextButton(
                                onPressed: (){
                                  showModalBottomSheet(
                                    isDismissible: false,
                                    enableDrag: false,
                                    context: context,
                                    builder: (BuildContext context)=> AddMenuScreen(buttonText: "Menü Ekle",route: "homeScreen",),
                                  );
                                },
                                child: TitleBorderedText(title: "LEZZET GRUBU EKLE",textColor: Colors.white,)
                        ),
                      ),
                    ),
                  ],
                )
              ],
            ),
          )
        ),
      ),
      onWillPop: ()async{
        var response = await showAlertDialog(context);
        print(response);
        return response;
      },
    );
  }
}
import 'package:hive/hive.dart';
import 'package:lezzet_kitabi/subCategoryModel.dart';

part 'categoryModel.g.dart';

@HiveType(typeId : 0)
class CategoryModel extends HiveObject{
  CategoryModel(
      { this.categoryId,
        this.categoryImagePath,
        this.categoryName,
        this.categoryColor,
        this.subCategoryModels});

  @HiveField(0)
  final int categoryColor;

  @HiveField(1)
  List <SubCategoryModel> subCategoryModels;

  @HiveField(2)
  int categoryId;

  @HiveField(3)
  String categoryImagePath;

  @HiveField(4)
  String categoryName;

}
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'categoryModel.dart';

// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************

class CategoryModelAdapter extends TypeAdapter<CategoryModel> {
  @override
  final int typeId = 0;

  @override
  CategoryModel read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return CategoryModel(
      categoryId: fields[2] as int,
      categoryImagePath: fields[3] as String,
      categoryName: fields[4] as String,
      categoryColor: fields[0] as int,
      subCategoryModels: (fields[1] as List)?.cast<SubCategoryModel>(),
    );
  }

  @override
  void write(BinaryWriter writer, CategoryModel obj) {
    writer
      ..writeByte(5)
      ..writeByte(0)
      ..write(obj.categoryColor)
      ..writeByte(1)
      ..write(obj.subCategoryModels)
      ..writeByte(2)
      ..write(obj.categoryId)
      ..writeByte(3)
      ..write(obj.categoryImagePath)
      ..writeByte(4)
      ..write(obj.categoryName);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is CategoryModelAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}


void main() async{
  WidgetsFlutterBinding.ensureInitialized();

  Directory document = await getApplicationDocumentsDirectory();
  Hive
    ..init(document.path)
    ..registerAdapter(CategoryModelAdapter())
    ..registerAdapter(SubCategoryModelAdapter())
    ..registerAdapter(IngredientAdapter());
  await Hive.openBox<CategoryModel>('categoryModelsInBox');

  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

  runApp(MyApp());
}

class LocalStorageService {
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: IngredientsProvider()),
        ChangeNotifierProvider.value(value: CompletedPageInfoProvider()),
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        initialRoute: WelcomeScreen.id,
        routes: {
          SubCategoriesScreen.id:(context)=>SubCategoriesScreen(),
          AddMenuScreen.id:(context)=>AddMenuScreen(),
          WelcomeScreen.id:(context)=>WelcomeScreen(),
          HomeScreen.id:(context)=>HomeScreen(),
          RecipeScreen.id:(context)=>RecipeScreen(),
          CompletedRecipeScreen.id:(context)=>CompletedRecipeScreen()
        },
      ),
    );
  }
}
4

0 回答 0