如何解决为什么 futureBuilder 返回空值?
当我尝试在我的主页中获取一个对象时,我没有收到错误,但即使我在下一个屏幕中使用相同的方法从数据库中显示我的对象,-
也不能取任何值。
FutureBuilder
我可以在这里显示我的对象而不会出现任何错误。我在下面使用相同的数据库和相同的构建器,但我可以取值。
@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: FutureBuilder<Map>(
future: HiveHelper().getCategoryModels(),builder: (BuildContext context,AsyncSnapshot<Map> snapshot){
if(snapshot.hasData) {
_list = snapshot.data.values.toList();
}
Future.delayed(Duration(milliseconds: 200));
return 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: _list.length+1,itemBuilder: (context,index){
if(_list.length==0){
return EmptyCard(where: "homeScreen",);
}
if(_flag==1){
return EmptyCard(where: "homeScreen",);
}
if(index==_list.length-1){
_flag=1;
CategoryModel categoryModel = _list[index];
return CategoryCard(category: categoryModel);
}
else{
CategoryModel categoryModel =_list[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;
},);
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: FutureBuilder<Map>(
future: HiveHelper().getCategoryModel(widget.categoryId),AsyncSnapshot<Map> snapshot) {
if(snapshot.hasData) {
_list=snapshot.data.values.toList();
}
Future.delayed(Duration(milliseconds: 200));
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,centerTitle: true,title: BorderedText(
child:Text(
_list[4],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:_list[5].length+1,index){
if(_list[5].length==0){
return EmptyCard(where: "subCategoryScreen",categoryId: widget.categoryId,);
}
if(index==_list[5].length-1){
_flag=1;
SubCategoryModel subCategoryModel =SubCategoryModel();
return SubCategoryCard(subCategoryCardId:index,subCategoryId:subCategoryModel.subCategoryId,subcategoryName: subCategoryModel.subCategoryName,subCategoryImagePath:subCategoryModel.subCategoryImagePath,subCategoryCardColor: subCategoryModel.categoryColor,);
}
else{
SubCategoryModel subCategoryModel =SubCategoryModel();
return SubCategoryCard(subCategoryCardId:index,);
}
}
),child: TextButton(
onpressed: (){
showModalBottomSheet(
isdismissible: false,builder: (BuildContext context)=> AddMenuScreen(categoryId:widget.categoryId,buttonText: "Tarif Ekle",route:"subCategoryScreen"),);
},child: BorderedText(
strokeWidth: 5,child:Text("Tarif Ekle",style: TextStyle(
color: Colors.white,fontFamily:'OpenSans',fontSize:30,);
}
),);
}
这是我的蜂巢课程。我该如何解决这个问题?
解决方法
我不确定,但你可以试试:
Future <Map> getCategoryModel(int index) async {
var box = await Hive.openBox('categoryModelsInBox');
var boxToMap=box.toMap();
return boxToMap["$index"];
}
,
不要在 FutureBuilder 的“future:”参数中创建未来。正如 Fine Manual 的第一大段所说:
future 一定是更早获得的,例如在 State.initState、State.didUpdateWidget 或 State.didChangeDependencies 期间。在构造 FutureBuilder 时,它不能在 State.build 或 StatelessWidget.build 方法调用期间创建。如果future和FutureBuilder同时创建,那么每次重建FutureBuilder的parent时,异步任务都会重新启动。
一般准则是假设每个构建方法都可以在每一帧被调用,并将省略的调用视为优化。
https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。