Flutter 颤振:如果值为null或没有值,如何隐藏TableRow
我有两个JSON案例,如下所示 案例1:“国家”的一个值为空 案例2:“国家”一词没有价值Flutter 颤振:如果值为null或没有值,如何隐藏TableRow,flutter,dart,Flutter,Dart,我有两个JSON案例,如下所示 案例1:“国家”的一个值为空 案例2:“国家”一词没有价值 [ { "id": 1, "continent": "North America", "country": "United States" }, { "id": 2, "continent": "Europe", "country": "Germany" }, { "id": 3, "continent": "Asia"
[
{
"id": 1,
"continent": "North America",
"country": "United States"
},
{
"id": 2,
"continent": "Europe",
"country": "Germany"
},
{
"id": 3,
"continent": "Asia"
}
]
我使用表格行
在表格
中显示“大陆”和“国家”
TableRow(children: [
TableCell(child: Text(continent.continent)),
TableCell(child: Text(continent.country))])
但如果列表或大陆中没有“country”。country==null
=>我不想显示表格行
,请帮助我设置以下条件:
import 'package:ask/services/continent_services2.dart';
import 'package:flutter/material.dart';
import 'model/continent_model2.dart';
class TableRowDemo extends StatefulWidget {
TableRowDemo() : super();
@override
_ContinentPageState createState() => _ContinentPageState();
}
class _ContinentPageState extends State<TableRowDemo> {
List<Continent> _continent;
@override
void initState() {
super.initState();
ContinentServices2.getContinent().then((continents) {
setState(() {
_continent = continents;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('')),
body: Column(
children: <Widget>[
for (Continent continent in _continent)
SingleChildScrollView(
child: Table(children: [
continent.country == null
? Container() // Error: type 'Container' is not a subtype of type 'TableRow'
: TableRow(children: [
TableCell(child: Text(continent.continent)),
TableCell(child: Text(continent.country))]),
]),
),
],
),
);
}
}
import'包:ask/services/contraction_services2.dart';
进口“包装:颤振/材料.省道”;
导入“模型/大陆_模型2.dart”;
类TableRowDemo扩展StatefulWidget{
TableRowDemo():super();
@凌驾
_ContinentPageState createState()=>\u ContinentPageState();
}
类PageState扩展了状态{
欧洲大陆名单;
@凌驾
void initState(){
super.initState();
大陆服务2.getContinental().then((大陆){
设置状态(){
_大陆=大陆;
});
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:文本(“”)),
正文:专栏(
儿童:[
对于(大陆中的大陆)
SingleChildScrollView(
子对象:表(子对象:[
constration.country==null
?Container()//错误:“Container”类型不是“TableRow”类型的子类型
:TableRow(儿童:[
TableCell(子项:文本(洲、洲)),
TableCell(子项:文本(大陆国家))],
]),
),
],
),
);
}
}
如果您升级到最新的颤振SDK(Dart>=2.3)
则可以使用简单的If
语句有条件地呈现TableRow或任何其他小部件
...
If(continent.country != null)
TableRow(children: [
TableCell(child: Text(continent.continent)),
TableCell(child: Text(continent.country))]),
]
下面是实现过滤的完整示例:
import 'package:flutter/material.dart';
void main() {
runApp(TableExample());
}
class TableExample extends StatefulWidget {
@override
_TableExampleState createState() => _TableExampleState();
}
class _TableExampleState extends State<TableExample> {
List continents = [
{"id": 1, "continent": "North America", "country": "United States"},
{"id": 2, "continent": "Europe", "country": "Germany"},
{"id": 3, "continent": "Asia", "country": null}
];
List _continents = [];
@override
void initState() {
super.initState();
// filtering of data
setState(() {
_continents =
continents.where((element) => element["country"] != null).toList();
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Table Example"),
),
body: Column(
children: <Widget>[
for (Map<String, dynamic> continent in _continents)
SingleChildScrollView(
child: Table(children: [
TableRow(children: [
TableCell(child: Text(continent["continent"])),
TableCell(child: Text(continent["country"]))
]),
]),
),
],
),
));
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(TableExample());
}
类TableExample扩展了StatefulWidget{
@凌驾
_TableExampleState createState()=>\u TableExampleState();
}
类_TableExampleState扩展状态{
列出大陆=[
{“id”:1,“大陆”:“北美”,“国家”:“美国”},
{“id”:2,“大陆”:“欧洲”,“国家”:“德国”},
{“id”:3,“大陆”:“亚洲”,“国家”:空}
];
列表_大陆=[];
@凌驾
void initState(){
super.initState();
//数据过滤
设置状态(){
_大陆=
大陆。其中((元素)=>element[“country”]!=null)。toList();
});
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
家:脚手架(
appBar:appBar(
标题:文本(“表格示例”),
),
正文:专栏(
儿童:[
用于(在_大洲中绘制大洲地图)
SingleChildScrollView(
子对象:表(子对象:[
TableRow(儿童:[
TableCell(子项:文本(大陆[“大陆])),
TableCell(子项:文本(大陆[“国家]))
]),
]),
),
],
),
));
}
}
输出:
您应该将大陆重命名为“zone”或“area”,您可以使用一个简单的if条件:
(…)
表(儿童:[
用于(分区中的分区)
如果(zone.country!=null)
TableRow(儿童)[
TableCell(子项:文本(zone.Continental)),
TableCell(子项:文本(zone.country)),
])
)
我检查了我的Dart版本:工具•Dart 2.8.2
,我尝试了你的代码,但在显示时显示错误,我有没有做错什么?你可以做更多的事情来避免这个错误,同时设置“过滤掉任何字段中有空的项”的值。这样你就不会有没有空元素的数据。Co你能帮我更新答案,这样我就可以复制粘贴它吗?我是一个代码新手:填充过滤的完整示例,请检查。你会收到初始错误,因为你需要用空数组初始化List _Continental,如->List _Continental=[];初始化它,希望该错误会消失。如果没有,则按照上面的示例过滤掉数据。我刚刚更新了我的答案。尝试一下。我希望它能工作@KelWait!!!它!!!如何???这是一个奇迹,你能告诉我为什么吗。它真的能工作,谢谢xD
import 'package:flutter/material.dart';
void main() {
runApp(TableExample());
}
class TableExample extends StatefulWidget {
@override
_TableExampleState createState() => _TableExampleState();
}
class _TableExampleState extends State<TableExample> {
List continents = [
{"id": 1, "continent": "North America", "country": "United States"},
{"id": 2, "continent": "Europe", "country": "Germany"},
{"id": 3, "continent": "Asia", "country": null}
];
List _continents = [];
@override
void initState() {
super.initState();
// filtering of data
setState(() {
_continents =
continents.where((element) => element["country"] != null).toList();
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Table Example"),
),
body: Column(
children: <Widget>[
for (Map<String, dynamic> continent in _continents)
SingleChildScrollView(
child: Table(children: [
TableRow(children: [
TableCell(child: Text(continent["continent"])),
TableCell(child: Text(continent["country"]))
]),
]),
),
],
),
));
}
}