Unable to pass data to new screen on click
I've a list of news displayed using card
widget and want to show details of corresponding news in new screen when user taps on any news. However, somehow the data I am trying to pass to new screen is null
and not sure where the issue is. Error I am getting is:
The getter 'urlToImage' was called on null.
If I understand this error correctly, the news
object I am passing is returning null
but how do I use that object to send actual data using Navigator
?
News model:
class News {
String title;
String description;
String urlToImage;
String content;
News(
this.title,
this.description,
this.urlToImage,
this.content
);
}
Card widget showing image and title of news:
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 is called as below:
class _NewsClassState extends State<NewsClass> {
final List<NewsCard> _news = <NewsCard>;
Details screen
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}')
),
]
)
);
}
}


add a comment |
I've a list of news displayed using card
widget and want to show details of corresponding news in new screen when user taps on any news. However, somehow the data I am trying to pass to new screen is null
and not sure where the issue is. Error I am getting is:
The getter 'urlToImage' was called on null.
If I understand this error correctly, the news
object I am passing is returning null
but how do I use that object to send actual data using Navigator
?
News model:
class News {
String title;
String description;
String urlToImage;
String content;
News(
this.title,
this.description,
this.urlToImage,
this.content
);
}
Card widget showing image and title of news:
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 is called as below:
class _NewsClassState extends State<NewsClass> {
final List<NewsCard> _news = <NewsCard>;
Details screen
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}')
),
]
)
);
}
}


where you have initialised news object ??
– Ajay Beniwal
Jan 2 at 12:58
I've initialized it insideNewsCard
class asNews news;
. @AjayBeniwal
– DK15
Jan 2 at 13:08
2
News news will not initialize it is just a null pointer News references you have to initalise using new keyword the problem you are getting because you are passing null object to the material page route
– Ajay Beniwal
Jan 2 at 13:31
Got it. I am now seeing the data in new screen, although I had to tweak code a bit. @AjayBeniwal
– DK15
Jan 2 at 13:41
add a comment |
I've a list of news displayed using card
widget and want to show details of corresponding news in new screen when user taps on any news. However, somehow the data I am trying to pass to new screen is null
and not sure where the issue is. Error I am getting is:
The getter 'urlToImage' was called on null.
If I understand this error correctly, the news
object I am passing is returning null
but how do I use that object to send actual data using Navigator
?
News model:
class News {
String title;
String description;
String urlToImage;
String content;
News(
this.title,
this.description,
this.urlToImage,
this.content
);
}
Card widget showing image and title of news:
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 is called as below:
class _NewsClassState extends State<NewsClass> {
final List<NewsCard> _news = <NewsCard>;
Details screen
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}')
),
]
)
);
}
}


I've a list of news displayed using card
widget and want to show details of corresponding news in new screen when user taps on any news. However, somehow the data I am trying to pass to new screen is null
and not sure where the issue is. Error I am getting is:
The getter 'urlToImage' was called on null.
If I understand this error correctly, the news
object I am passing is returning null
but how do I use that object to send actual data using Navigator
?
News model:
class News {
String title;
String description;
String urlToImage;
String content;
News(
this.title,
this.description,
this.urlToImage,
this.content
);
}
Card widget showing image and title of news:
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 is called as below:
class _NewsClassState extends State<NewsClass> {
final List<NewsCard> _news = <NewsCard>;
Details screen
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}')
),
]
)
);
}
}




asked Jan 2 at 12:06
DK15DK15
206113
206113
where you have initialised news object ??
– Ajay Beniwal
Jan 2 at 12:58
I've initialized it insideNewsCard
class asNews news;
. @AjayBeniwal
– DK15
Jan 2 at 13:08
2
News news will not initialize it is just a null pointer News references you have to initalise using new keyword the problem you are getting because you are passing null object to the material page route
– Ajay Beniwal
Jan 2 at 13:31
Got it. I am now seeing the data in new screen, although I had to tweak code a bit. @AjayBeniwal
– DK15
Jan 2 at 13:41
add a comment |
where you have initialised news object ??
– Ajay Beniwal
Jan 2 at 12:58
I've initialized it insideNewsCard
class asNews news;
. @AjayBeniwal
– DK15
Jan 2 at 13:08
2
News news will not initialize it is just a null pointer News references you have to initalise using new keyword the problem you are getting because you are passing null object to the material page route
– Ajay Beniwal
Jan 2 at 13:31
Got it. I am now seeing the data in new screen, although I had to tweak code a bit. @AjayBeniwal
– DK15
Jan 2 at 13:41
where you have initialised news object ??
– Ajay Beniwal
Jan 2 at 12:58
where you have initialised news object ??
– Ajay Beniwal
Jan 2 at 12:58
I've initialized it inside
NewsCard
class as News news;
. @AjayBeniwal– DK15
Jan 2 at 13:08
I've initialized it inside
NewsCard
class as News news;
. @AjayBeniwal– DK15
Jan 2 at 13:08
2
2
News news will not initialize it is just a null pointer News references you have to initalise using new keyword the problem you are getting because you are passing null object to the material page route
– Ajay Beniwal
Jan 2 at 13:31
News news will not initialize it is just a null pointer News references you have to initalise using new keyword the problem you are getting because you are passing null object to the material page route
– Ajay Beniwal
Jan 2 at 13:31
Got it. I am now seeing the data in new screen, although I had to tweak code a bit. @AjayBeniwal
– DK15
Jan 2 at 13:41
Got it. I am now seeing the data in new screen, although I had to tweak code a bit. @AjayBeniwal
– DK15
Jan 2 at 13:41
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006062%2funable-to-pass-data-to-new-screen-on-click%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006062%2funable-to-pass-data-to-new-screen-on-click%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
where you have initialised news object ??
– Ajay Beniwal
Jan 2 at 12:58
I've initialized it inside
NewsCard
class asNews news;
. @AjayBeniwal– DK15
Jan 2 at 13:08
2
News news will not initialize it is just a null pointer News references you have to initalise using new keyword the problem you are getting because you are passing null object to the material page route
– Ajay Beniwal
Jan 2 at 13:31
Got it. I am now seeing the data in new screen, although I had to tweak code a bit. @AjayBeniwal
– DK15
Jan 2 at 13:41