如何解决Hive 包不保存主对象下的对象
您好,我有一个应用程序,用户可以为主要类别创建子类别。当用户创建主类别时,主对象保存到数据库中,在我重新打开应用程序后我可以看到它们,但是当我为指定的主类别创建子类别时,我可以在子类别屏幕中看到,但是当我重新打开时该应用程序我可以看到唯一的主要类别,我猜子类别无法保存到数据库中,我使用 Hive 将我的数据存储到手机中,有人可以解释为什么我不能将子对象保存到数据库中,我该怎么做那。我已经坚持了谢谢你的帮助..
我共享主要类别屏幕,我让它们显示主要类别
class HomeScreen extends StatefulWidget {
HomeScreen({this.newCategoryName,this.newCategoryImagePath});
final String newCategoryImagePath;
final String newCategoryName;
static String id="homeScreen";
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
Widget buildBottomSheet(BuildContext context)=>AddMenuScreen(buttonText: "Menü Ekle",route: "homeScreen");
int _flag=0;
Box<CategoryModel> _categoryModelsBox;
void initState(){
super.initState();
_categoryModelsBox = Hive.Box<CategoryModel>("categoryModelsInBox");
if (widget.newCategoryName!=null && widget.newCategoryImagePath!=null){
CategoryModel newCategoryModel = CategoryModel(categoryId:categoryModels.length,categoryImagePath: widget.newCategoryImagePath,categoryName: widget.newCategoryName,categoryColor: Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(0.5).value);
categoryModels.insert(categoryModels.length,newCategoryModel);
_categoryModelsBox.add(newCategoryModel);
}
}
Future<bool> showAlertDialog(BuildContext context){
AlertDialog alert = AlertDialog(
backgroundColor: kColorTheme10,elevation: 0,content: Container(decoration:Boxdecoration(
border: Border.all(style: BorderStyle.solid,width: 1),color: Colors.white70.withOpacity(0.7),borderRadius: BorderRadius.circular(15)
),child: Padding(
padding: EdgeInsets.all(8.0),child: Text(
"Çıkmak istediğinizden emin misiniz?",style: TextStyle(
fontSize: 25,fontFamily: "OpenSans")
),),shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),actions: [
Container(
decoration: Boxdecoration(
color: Colors.white70.withOpacity(0.7),borderRadius: BorderRadius.circular(30)
),child: TextButton(onpressed:()async{
Navigator.of(context).pop(true);
},child: Padding(
padding: const EdgeInsets.all(8.0),child: Text("EVET",style: TextStyle(fontWeight: FontWeight.bold,fontFamily: "OpenSans"),)),Container(
decoration: Boxdecoration(
color:Colors.white70.withOpacity(0.7),child: TextButton(onpressed:()async{
Navigator.of(context).pop(false);
},child: Text("HAYIR",fontFamily: "OpenSans")),],);
return showDialog(
context: context,builder: (_)=>alert);
}
@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;
},);
}
}
我共享主类别屏幕,我让它们显示主类别的子类别
class SubCategoriesScreen extends StatefulWidget {
SubCategoriesScreen({this.categoryId,this.subCategoryName,this.subCategoryImagePath});
static String id="subCategoriesScreen";
final String subCategoryName;
final String subCategoryImagePath;
final int categoryId;
Widget buildBottomSheet(BuildContext context)=>AddMenuScreen(categoryId: categoryId,buttonText: "Tarif Ekle",route: "subCategoryScreen",);
@override
_SubCategoriesScreenState createState() => _SubCategoriesScreenState();
}
class _SubCategoriesScreenState extends State<SubCategoriesScreen> {
int _flag=0;
Box<CategoryModel> _categoryModelBox;
CategoryModel _categoryModel;
void initState(){
super.initState();
_categoryModelBox = Hive.Box<CategoryModel>("categoryModelsInBox");
_categoryModel = _categoryModelBox.getAt(widget.categoryId);
if(widget.categoryId!=null && widget.subCategoryImagePath!=null && widget.subCategoryName!=null){
addSubCategory();
}
}
void addSubCategory()async{
SubCategoryModel subCategoryModel=SubCategoryModel(
subCategoryId:widget.categoryId,subCategoryImagePath: widget.subCategoryImagePath,subCategoryName: widget.subCategoryName,categoryColor: Color((Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(0.5).value);
if(_categoryModel.subCategoryModels!=null){
//for (var categoryModel in categoryModels){
//if (categoryModel.categoryId == subCategoryModel.subCategoryId){
//categoryModel.subCategoryModels.insert(categoryModel.subCategoryModels.length-1,subCategoryModel);
//categoryModel.subCategoryModels.add(subCategoryModel);
_categoryModel=_categoryModelBox.getAt(widget.categoryId);
_categoryModel.subCategoryModels.add(subCategoryModel);
_categoryModelBox.putAt(widget.categoryId,_categoryModel);
//}
//}
}
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,centerTitle: true,title: BorderedText(
child:Text(
_categoryModel.categoryName,style: TextStyle(
color: Color(0XFFFFFB00),fontSize: 30,fontFamily: "OpenSans"
),strokeWidth: 5,strokeColor: Colors.black,elevation: 5,leading: IconButton(
icon: Icon(Icons.arrow_back),onpressed: (){
Navigator.pop(context);
},iconSize: 40,color: Color(0xFFA2000B),actions: [
CircleAvatar(
radius: 27,backgroundImage: Assetimage("images/cuttedlogo.PNG"),)
],body:Container(
decoration: Boxdecoration(
image: decorationImage(
image: Assetimage("images/logoBGopacity.png"),children: [
Expanded(
child: GridView.builder(
scrollDirection: Axis.vertical,itemCount:_categoryModel.subCategoryModels.length+1,index){
if(_categoryModel.subCategoryModels.length==0){
return EmptyCard(where: "subCategoryScreen",categoryId: widget.categoryId,);
}
if(_flag==1){
return EmptyCard(where: "subCategoryScreen",);
}
if(index==_categoryModel.subCategoryModels.length-1){
_flag=1;
SubCategoryModel subCategoryModel =_categoryModel.subCategoryModels[index];
return SubCategoryCard(subCategoryCardId:index,subCategoryId:subCategoryModel.subCategoryId,subcategoryName: subCategoryModel.subCategoryName,subCategoryImagePath:subCategoryModel.subCategoryImagePath,subCategoryCardColor: subCategoryModel.categoryColor,);
}
else{
SubCategoryModel subCategoryModel =_categoryModel.subCategoryModels[index];
return SubCategoryCard(subCategoryCardId:index,);
}
}
),child: TextButton(
onpressed: (){
showModalBottomSheet(
isdismissible: false,builder: (BuildContext context)=> AddMenuScreen(categoryId:widget.categoryId,route:"subCategoryScreen"),);
},child: BorderedText(
strokeWidth: 5,child:Text("Tarif Ekle",style: TextStyle(
color: Colors.white,fontFamily:'OpenSans',fontSize:30,)
),);
}
}
part 'categoryModel.g.dart';
@HiveType(typeId : 0)
class CategoryModel extends HiveObject{
CategoryModel(
{ this.categoryId,this.categoryImagePath,this.categoryName,this.categoryColor});
@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 = 1;
@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;
}
part 'subCategoryModel.g.dart';
@HiveType(typeId : 1)
class SubCategoryModel extends HiveObject{
SubCategoryModel({
this.subCategoryId,this.subCategoryImagePath,this.categoryColor,this.recipeId,List<Ingredient>ingredients,this.recipePhotoDir,this.recordedVoiceDir,bool isCompeted});
@HiveField(0)
final int categoryColor;
@HiveField(1)
final double recipeId;
@HiveField(2)
bool isCompleted;
@HiveField(3)
int subCategoryId;
@HiveField(4)
String subCategoryImagePath;
@HiveField(5)
String subCategoryName;
@HiveField(6)
List <Ingredient> ingredients=[] ;
@HiveField(7)
String recipePhotoDir;
@HiveField(8)
String recordedVoiceDir;
}
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'subCategoryModel.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class SubCategoryModelAdapter extends TypeAdapter<SubCategoryModel> {
@override
final int typeId = 2;
@override
SubCategoryModel read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int,};
return SubCategoryModel(
subCategoryId: fields[3] as int,subCategoryImagePath: fields[4] as String,subCategoryName: fields[5] as String,recipeId: fields[1] as double,ingredients: (fields[6] as List)?.cast<Ingredient>(),recipePhotoDir: fields[7] as String,recordedVoiceDir: fields[8] as String,)..isCompleted = fields[2] as bool;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。