card
当用户点击任何新闻时,希望在新屏幕上显示相应新闻的详细信息。然而,不知何故,我试图传递给新屏幕的数据是
null
也不知道问题出在哪里。我得到的错误是:
The getter 'urlToImage' was called on null.
如果我正确地理解了这个错误
news
无效的
但如何使用该对象发送实际数据
Navigator
?
新闻模式:
class News {
String title;
String description;
String urlToImage;
String content;
News(
this.title,
this.description,
this.urlToImage,
this.content
);
}
显示新闻图片和标题的卡片小部件:
class NewsCard extends StatelessWidget{
final String title, urlToImage;
News news;
NewsCard({this.title,this.urlToImage});
final makeListTile = ListTile(
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => new Details(news)));
},
contentPadding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
leading: Container(
padding: EdgeInsets.only(right: 12.0),
decoration: BoxDecoration(
border: Border(
right: BorderSide(
width: 1.0,
),
),
),
child: Image(
height: 50.0,
width: 50.0,
fit: BoxFit.cover,
image: NetworkImage(urlToImage),
),
),
title: Text(title, style: TextStyle(
fontWeight: FontWeight.bold
)),
trailing: Icon(Icons.keyboard_arrow_right, size: 30.0)
);
return
Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.0)),
elevation: 8.0,
margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
child: Container(
child: makeListTile,
)
);
}
}
NewsCard
class _NewsClassState extends State<NewsClass> {
final List<NewsCard> _news = <NewsCard>[];
详细信息屏幕
class Details extends StatelessWidget {
final News news;
Details(this.news);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('News Details'),
centerTitle: true,
),
body: ListView(
children: <Widget>[
Image.network('${news.urlToImage}',fit: BoxFit.cover,),
Container(
child: Text('${news.content}')
),
]
)
);
}
}