List flatter如何在init状态下使用未来的异步方法
当屏幕加载时,我希望init状态用通过有状态小部件传递的Future填充列表。然而,由于这是一个未来,我的方法将崩溃,无法工作 我的代码List flatter如何在init状态下使用未来的异步方法,list,firebase,flutter,future,List,Firebase,Flutter,Future,当屏幕加载时,我希望init状态用通过有状态小部件传递的Future填充列表。然而,由于这是一个未来,我的方法将崩溃,无法工作 我的代码 @override void initState() async { FutureBuilder( future: widget.imageList, builder: (context, snapshot) { switch (snapshot.connectionState) {
@override
void initState() async {
FutureBuilder(
future: widget.imageList,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
print('NONE');
break;
case ConnectionState.active:
case ConnectionState.waiting:
print("WAITING");
break;
case ConnectionState.done:
print("DONE");
setState(() {
imgList = widget.imageList as List<String>;
});
break;
}
},
);
super.initState();
}
传递列表的有状态小部件
List<String> imgList = List<String>();
class BottomSheetWidget extends StatefulWidget {
BottomSheetWidget({Key key, this.name, this.imageList}) : super(key: key);
String name;
Future<List<String>> imageList;
List imgList=List();
类BottomSheetWidget扩展了StatefulWidget{
BottomSheetWidget({Key-Key,this.name,this.imageList}):super(Key:Key);
字符串名;
未来图像列表;
全部编辑代码:
List<String> imgList = List<String>();
class BottomSheetWidget extends StatefulWidget {
BottomSheetWidget({Key key, this.name, this.imageList}) : super(key: key);
String name;
Future<List<String>> imageList;
@override
_BottomSheetWidgetState createState() => _BottomSheetWidgetState();
}
class _BottomSheetWidgetState extends State<BottomSheetWidget> {
int _current = 0;
final List<Widget> imageSliders = imgList
.map((item) => Container(
child: Container(
margin: EdgeInsets.all(5.0),
child: ClipRRect(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
child: Stack(
children: <Widget>[
Image.network(item,
fit: BoxFit.cover, height: 1000.0, width: 1000.0),
Positioned(
bottom: 0.0,
left: 0.0,
right: 0.0,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color.fromARGB(200, 0, 0, 0),
Color.fromARGB(0, 0, 0, 0)
],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
),
),
),
],
)),
),
))
.toList();
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: widget.imageList,
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
print('NONE');
break;
case ConnectionState.active:
case ConnectionState.waiting:
print("WAITING");
break;
case ConnectionState.done:
print("DONNE");
setState(() async {
imgList = snapshot.data as List<String>;
});
return Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(25),
),
height: MediaQuery.of(context).size.height * 0.85,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
CarouselSlider(
items: imageSliders,
options: CarouselOptions(
enlargeCenterPage: true,
enlargeStrategy: CenterPageEnlargeStrategy.height,
height: MediaQuery.of(context).size.height - 500,
aspectRatio: 2.0,
onPageChanged: (index, reason) {
setState(() {
_current = index;
});
}),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: imgList.map((url) {
int index = imgList.indexOf(url);
return Container(
width: 8.0,
height: 8.0,
margin: EdgeInsets.symmetric(
vertical: 10.0, horizontal: 2.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _current == index
? Color.fromRGBO(0, 0, 0, 0.9)
: Color.fromRGBO(0, 0, 0, 0.4),
),
);
}).toList(),
),
Text(widget.name,
style: TextStyle(
fontSize: 30,
color: Colors.white,
fontWeight: FontWeight.bold)),
Text("United Kingdom",
style: TextStyle(
fontSize: 18,
color: Colors.white,
)),
],
),
)
],
),
);
break;
}
},
);
}
}
List imgList=List();
类BottomSheetWidget扩展了StatefulWidget{
BottomSheetWidget({Key-Key,this.name,this.imageList}):super(Key:Key);
字符串名;
未来图像列表;
@凌驾
_BottomSheetWidgetState createState()=>\u BottomSheetWidgetState();
}
类WidgetState扩展了状态{
int _电流=0;
最终列表imageSliders=imgList
.map((项)=>容器(
子:容器(
边距:所有边缘集(5.0),
孩子:ClipRRect(
borderRadius:borderRadius.all(半径.圆形(5.0)),
子:堆栈(
儿童:[
图像。网络(项目,
尺寸:BoxFit.cover,高度:1000.0,宽度:1000.0),
定位(
底部:0.0,
左:0.0,
右:0.0,
子:容器(
装饰:盒子装饰(
梯度:线性梯度(
颜色:[
颜色。来自argb(200,0,0,0),
Color.fromARGB(0,0,0,0)
],
开始:对齐.bottomCenter,
结束:对齐。上止点,
),
),
),
),
],
)),
),
))
.toList();
@凌驾
小部件构建(构建上下文){
回归未来建设者(
未来:widget.imageList,
生成器:(上下文,快照){
交换机(快照.连接状态){
案例连接状态。无:
打印(“无”);
打破
案例连接状态.active:
案例连接状态。正在等待:
打印(“等待”);
打破
案例连接状态。完成:
印刷品(“DONNE”);
setState(()异步{
imgList=snapshot.data作为列表;
});
返回容器(
装饰:盒子装饰(
颜色:颜色,红色,
边界半径:边界半径。圆形(25),
),
高度:MediaQuery.of(上下文).size.height*0.85,
宽度:MediaQuery.of(context).size.width,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
mainAxisSize:mainAxisSize.max,
儿童:[
填充物(
填充:边缘组。对称(水平:15),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
旋转滑翔机(
项目:图像滑块,
选项:旋转木马(
放大中心页:正确,
放大策略:CenterPageEnlargeStrategy.height,
高度:MediaQuery.of(context).size.height-500,
aspectRatio:2.0,
onPageChanged:(索引,原因){
设置状态(){
_电流=指数;
});
}),
),
划船(
mainAxisAlignment:mainAxisAlignment.center,
子项:imgList.map((url){
int index=imgList.indexOf(url);
返回容器(
宽度:8.0,
身高:8.0,
边距:边缘组。对称(
垂直:10.0,水平:2.0),
装饰:盒子装饰(
形状:BoxShape.circle,
颜色:_当前==索引
?颜色。来自RGBO(0,0,0,0.9)
:颜色。来自RGBO(0,0,0,0.4),
),
);
}).toList(),
),
Text(widget.name,
样式:TextStyle(
尺寸:30,
颜色:颜色,白色,
fontWeight:fontWeight.bold),
案文(“联合王国”,
样式:TextStyle(
尺码:18,
颜色:颜色,白色,
)),
],
),
)
],
),
);
打破
}
},
);
}
}
来自控制台的新错误:
flutter: WAITING
════════ Exception caught by widgets library ═══════════════════════════════════
A build function returned null.
The relevant error-causing widget was
FutureBuilder<List<String>>
lib/common_widget/bottom_sheet.dart:54
════════════════════════════════════════════════════════════════════════════════
flutter: DONNE
════════ Exception caught by widgets library ═══════════════════════════════════
setState() callback argument returned a Future.
The relevant error-causing widget was
FutureBuilder<List<String>>
lib/common_widget/bottom_sheet.dart:54
════════════════════════════════════════════════════════════════════════════════
flatter:等待
════════ widgets库捕获到异常═══════════════════════════════════
生成函数返回null。
导致错误的相关小部件已被删除
未来建设者
lib/common_widget/bottom_sheet.dart:5
flutter: WAITING
════════ Exception caught by widgets library ═══════════════════════════════════
A build function returned null.
The relevant error-causing widget was
FutureBuilder<List<String>>
lib/common_widget/bottom_sheet.dart:54
════════════════════════════════════════════════════════════════════════════════
flutter: DONNE
════════ Exception caught by widgets library ═══════════════════════════════════
setState() callback argument returned a Future.
The relevant error-causing widget was
FutureBuilder<List<String>>
lib/common_widget/bottom_sheet.dart:54
════════════════════════════════════════════════════════════════════════════════
import 'package:flutter/material.dart';
class BottomSheetWidget extends StatefulWidget {
BottomSheetWidget({Key key, this.name, this.imageList}) : super(key: key);
final String name;
final Future<List<String>> imageList;
@override
_BottomSheetWidgetState createState() => _BottomSheetWidgetState();
}
class _BottomSheetWidgetState extends State<BottomSheetWidget> {
int _current = 0;
@override
Widget build(BuildContext context) {
return FutureBuilder<List<String>>(
future: widget.imageList,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final imgList = snapshot.data;
return Container(
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(25),
),
height: MediaQuery.of(context).size.height * 0.85,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 15),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
CarouselSlider(
items: imgList.map(
(item) {
return Container(
child: Container(
margin: EdgeInsets.all(5.0),
child: ClipRRect(
borderRadius:
BorderRadius.all(Radius.circular(5.0)),
child: Stack(
children: <Widget>[
Image.network(
item,
fit: BoxFit.cover,
height: 1000.0,
width: 1000.0,
),
Positioned(
bottom: 0.0,
left: 0.0,
right: 0.0,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
Color.fromARGB(200, 0, 0, 0),
Color.fromARGB(0, 0, 0, 0)
],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
),
),
),
],
),
),
),
);
},
).toList(),
options: CarouselOptions(
enlargeCenterPage: true,
enlargeStrategy: CenterPageEnlargeStrategy.height,
height: MediaQuery.of(context).size.height - 500,
aspectRatio: 2.0,
onPageChanged: (index, reason) {
setState(() {
_current = index;
});
},
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: imgList.map((url) {
return Container(
width: 8.0,
height: 8.0,
margin: EdgeInsets.symmetric(
vertical: 10.0, horizontal: 2.0),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: _current == imgList.indexOf(url)
? Color.fromRGBO(0, 0, 0, 0.9)
: Color.fromRGBO(0, 0, 0, 0.4),
),
);
}).toList(),
),
Text(
widget.name,
style: TextStyle(
fontSize: 30,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
Text(
"United Kingdom",
style: TextStyle(
fontSize: 18,
color: Colors.white,
),
),
],
),
)
],
),
);
} else {
return CircularProgressIndicator();
}
},
);
}
}