Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振应用程序在重新打开后与Hive DB发生问题_Flutter_Flutter Hive - Fatal编程技术网

Flutter 颤振应用程序在重新打开后与Hive DB发生问题

Flutter 颤振应用程序在重新打开后与Hive DB发生问题,flutter,flutter-hive,Flutter,Flutter Hive,我在我的应用程序中使用hive DB,当我第一次运行代码时一切正常(我可以添加主类别,也可以添加子类别),但在关闭并打开我的应用程序后,我可以看到我在关闭应用程序之前保存的所有类,但当我在重新打开应用程序后尝试添加新类别时,应用程序会在我添加的新类别中显示firts category的项目。为什么我的应用程序在我再次关闭和打开时出现问题。谢谢你的帮助 @override Widget build(BuildContext context) { return WillPopScop

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

  @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';
导入“包:lezzet_kitabi/subcategory model.dart”;
“类别模型g.dart”部分;
@HiveType(类型ID:0)
类CategoryModel扩展了HiveObject{
分类模型(
{this.categoryId,
此.categoryImagePath,
这个.categoryName,
这个,分类颜色,
这包括:子类别模型});
@HiveField(0)
最终int分类颜色;
@HiveField(1)
列出子类别模型;
@HiveField(2)
int分类;
@HiveField(3)
字符串类别图像路径;
@HiveField(4)
字符串类别名称;
}
//生成的代码-不要手动修改
“categoryModel.dart”的一部分;
// **************************************************************************
//类型适配器发生器
// **************************************************************************
类CategoryModelAdapter扩展了TypeAdapter{
@凌驾
最终int-typeId=0;
@凌驾
类别模型读取(二进制读取器){
final numofields=reader.readByte();
最终字段={
对于(inti=0;itypeId.hashCode;
@凌驾
布尔运算符==(对象其他)=>
相同的(这个,其他)||
另一个是CategoryModelAdapter&&
runtimeType==other.runtimeType&&
typeId==other.typeId;
}

void main()异步{
WidgetsFlutterBinding.ensureInitialized();
目录文档=等待getApplicationDocumentsDirectory();
蜂箱
..init(document.path)
..registerAdapter(CategoryModelAdapter())
…registerAdapter(子类别ModelAdapter())
..registerAdapter(IngRadientAdapter());
等待Hive.openBox('categoryModelsInBox');
SystemChrome.setPreferredOrientations(
[设备定向。肖像向上,设备定向。肖像向下];
runApp(MyApp());
}
类LocalStorageService{
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
回程多供应商(
供应商:[
ChangeNotifierProvider.value(值:IngredientsProvider()),
ChangeNotifierProvider.value(值:CompletedPageInfoProvider()),
],
孩子:MaterialApp(
debugShowCheckedModeBanner:false,
initialRoute:WelcomeScreen.id,
路线:{
SubCategoriesScreen.id:(上下文)=>SubCategoriesScreen(),
AddMenuScreen.id:(上下文)=>AddMenuScreen(),
WelcomeScreen.id:(上下文)=>WelcomeScreen(),
HomeScreen.id:(上下文)=>HomeScreen(),
RecipesScreen.id:(上下文)=>RecipesScreen(),
id:(上下文)=>CompletedRecipesScreen()
},
),
);
}
}
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()
        },
      ),
    );
  }
}