gpt4 book ai didi

flutter - 在 future builder 中使用列表构建器时 flutter 显示错误

转载 作者:行者123 更新时间:2023-12-03 03:50:43 25 4
gpt4 key购买 nike

我在Future builder中使用ListBuilder在类中传递快照数据,并在listBuilder的帮助下以列表形式显示所有内容,但一切正常,但在列表末尾显示错误的同时也在UI中显示错误
我的密码

Widget build(BuildContext context) {
double statusBarHeight = MediaQuery.of(context).padding.top;
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return Expanded(
child: FutureBuilder(
future: doSomeAsyncStuff(),
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
print('working');
print(snapshot.data);
return ListViewPosts(posts: snapshot);
}
}
return Center(
child: Text("Not Loaded Yet!!"),
);
}),
);
}

}

class ListViewPosts extends StatelessWidget {
final posts;

ListViewPosts({Key key, this.posts}) : super(key: key);

@override
Widget build(BuildContext context) {
double statusBarHeight = MediaQuery.of(context).padding.top;
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
return Expanded(
child: Container(
child: ListView.builder(
itemCount: posts.data.length,
padding: const EdgeInsets.all(15.0),
itemBuilder: (context, position) {
return Container(
height: height * 0.4,
child: GestureDetector(
child: Center(
child: Card(
margin: EdgeInsets.zero,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30),
),
elevation: 30,
child: Container(
child: Stack(
children: <Widget>[
Container(
width: width * 0.6,
height: height * 0.17,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/61.jpg',
),
fit: BoxFit.fill,
),
),
),
Padding(
padding: EdgeInsets.only(top: height * 0.17),
child: Container(
height: height * 0.15,
color: Colors.white,
width: MediaQuery.of(context).size.width * 0.6,
child: Padding(
padding: const EdgeInsets.only(
top: 30, bottom: 7, left: 15, right: 15),
child: Row(
children: <Widget>[
Flexible(
child: Text(
posts.data[position]['Description'],
style: TextStyle(
color: Color(0xff343A40),
fontFamily: 'OpenSans',
fontWeight: FontWeight.bold,
fontSize: 15),
),
),
],
),
),
),
),
Positioned.fill(
child: Align(
alignment: Alignment.centerLeft,
child: Container(
margin: EdgeInsets.only(left: width * 0.02),
child: ClipRRect(
borderRadius:
BorderRadius.all(Radius.circular(12)),
child: Container(
height: height * 0.08,
width: width * 0.4,
color: Color(0xff343A40),
child: Column(
mainAxisAlignment:
MainAxisAlignment.center,
crossAxisAlignment:
CrossAxisAlignment.center,
children: <Widget>[
Text(
posts.data[position]
['CreatedOn']
.substring(0, 10),
style: TextStyle(
fontSize: 20,
color: Color(0xffFFFFFF),
fontFamily: 'OpenSans',
fontWeight:
FontWeight.bold),
),
]),
),
),
)),
),
],
),
),
),
),
),
);
}),
),
);
}
}
错误是方法'substring'在null上被调用。
接收者:null
尝试调用:substring(0,10)
enter image description here

最佳答案

问题是,您的subString之一返回空值。并且您尝试在“文本”窗口小部件中传递空值。
请尝试以下代码:

Text(
posts.data[position]['CreatedOn'].substring(0, 10)??"Not available",
style: TextStyle(
fontSize: 20,
color: Color(0xffFFFFFF),
fontFamily: 'OpenSans',
fontWeight:FontWeight.bold),
),

关于flutter - 在 future builder 中使用列表构建器时 flutter 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63531845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com