gpt4 book ai didi

dart - Flutter:Future Builder 获取多个数据

转载 作者:IT王子 更新时间:2023-10-29 06:37:16 24 4
gpt4 key购买 nike

我正在查看我网站的新闻部分。我正在使用 Future Builder 从网络上获取数据。我遇到的问题与我尝试在屏幕上显示的图像有关。而且新闻多的时候,数据加载时间长,不知道有没有加载更快的解决方案。

我是通过一个json查询新闻的正文。在那一刻,您将获得另一个 JSON 的 URL,其中图像为缩略图格式。

我希望能解决这个问题,感谢任何帮助。

enter image description here

News.dart - 代码

  @override
Widget build(BuildContext context) {
return Scaffold(
appBar: searchBar.build(context),
key: _scaffoldKey,
body: new Container(
color: Colors.grey[800],
child: new RefreshIndicator(
child: new ListView(
children: <Widget>[
new FutureBuilder<List<Post>>(
future: fetchPosts(URLWEB),
builder: (context, snapshot) {
if(snapshot.hasData) {
List<Post> posts = snapshot.data;
return new Column(
children: posts.map((post2) => new Column(
children: <Widget>[
new Card(
margin: new EdgeInsets.symmetric(vertical: 20.0, horizontal: 20.0),
color: Colors.white,
child: new GestureDetector(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new FutureBuilder(
future: fetchPostsIMG(post2.imagen),
builder: (context, AsyncSnapshot<PostImg> snapshot2){
return new Container(
height: 200.0,
decoration: new BoxDecoration(
image: new DecorationImage(
image: CachedNetworkImageProvider(snapshot2.data.imagen == null ? new AssetImage('images/logotipo.png') : snapshot2.data.imagen),
fit: BoxFit.fitWidth
)
),
width: MediaQuery.of(context).size.width,
);
},
),
new ListTile(
title: new Text(post2.titulo.replaceAll("&#8216;", "").replaceAll(
"&#8217;", "").replaceAll("&#8211;", "")
.replaceAll("&#8230;", "").replaceAll(
"&#8221;", "")
.replaceAll("&#8220;", ""),
style: new TextStyle(
color: Colors.black,
fontSize: 18.0,
fontWeight: FontWeight.bold),),
subtitle: new HtmlView(data: post2.informacion),
dense: true,
)
],
),
onTap: () {
//Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context)=> new WebView(url: post2.urlweb, titulo: titulo)));
},
)
)
],
)).toList(),
);
}
else if(snapshot.hasError)
{
return new Container();
}
return new Center(
child: new Column(
children: <Widget>[
new Padding(padding: new EdgeInsets.all(50.0)),
new CircularProgressIndicator(),
],
),
);
},
),
],
),
onRefresh: _autoRefresh
),
),
);
}
}

最佳答案

这是因为您正试图访问空对象上的 imagen。你可以像下面那样做 hasData 检查

CachedNetworkImageProvider(snapshot2.hasData ? snapshot2.data.imagen : new AssetImage('images/logotipo.png')),

关于dart - Flutter:Future Builder 获取多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52406604/

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