Я новичок во Флаттере и Дартсе.
У меня есть локальный файл JSON, прочитайте его и я хочу отобразить данные JSON в ListView.
Но в моих данных JSON у меня не всегда есть все различные свойства.
Поэтому, когда я хочу отобразить текст, значение свойства не существует, потому что свойство не существует (В данном случае это свойство "описание".
Как я мог решить это?
Заранее благодарю за помощь
я пробовал троичный оператор
Я пробовал с функцией содержит ключ
Но, может быть, я сделал это вонг?
... json
[
{
"createdBy": "bddae0de-320c-41a9-a69b-75793758b7a7",
"description": "Fhjifgsdsdsd",
"endDateTime": "1477490400000",
"hasPicture": "false",
"isActive": "true",
"isAdminActive": "true",
"maxParticipants": "50",
"startDateTime": "1476799200000",
"title": "Song Church Story Telling",
"type": "Music"
},
{
"createdBy": "-KHzgxS6KBqu1rNmJzpT",
"endDateTime": "1476378000000",
"hasPicture": "false",
"isActive": "true",
"isAdminActive": "true",
"startDateTime":"1476205200000",
"title": "Test greg T",
"titleDate": "Tuesday, 11 Oct 8:00 PM",
"type": "Games"
}
]
...
... флаттер
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Load local JSON file"),
),
body: new Container(
child: new Center(
// Use future builder and DefaultAssetBundle to load the local JSON file
child: new FutureBuilder(
future: DefaultAssetBundle.of(context)
.loadString('assets/data/ckevents_data.json'),
builder: (context, snapshot) {
// Decode the JSON
var newData = json.decode(snapshot.data.toString());
return new ListView.builder(
// Build the ListView
itemBuilder: (BuildContext context, int index) {
return new Card(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
new Text("Title: " + newData[index]['title'],
style: new TextStyle(
fontSize: 20.0, color: Colors.blue)),
new Text(
"Description: " +
((newData[index].containsKey('description')) ? ('') : (newData[index]['description'])),
style: new TextStyle(
fontSize: 10.0, color: Colors.pink)),
new Text("Categorie: " + newData[index]['type'],
style: new TextStyle(
fontSize: 15.0, color: Colors.red)),
new Text(
"Date: " +
DateTime.fromMillisecondsSinceEpoch(
newData[index]['startDateTime'])
.add(Duration(days: 700))
.toString(),
style: new TextStyle(
fontSize: 10.0, color: Colors.black))
],
),
);
},
itemCount: newData == null ? 0 : newData.length,
);
}),
),
));
}
}
...