gpt4 book ai didi

flutter - Flutter:如果值为空或没有值,如何隐藏TableRow

转载 作者:行者123 更新时间:2023-12-03 02:52:25 25 4
gpt4 key购买 nike

我有2个JSON情况,如下所示

情况1:“国家”的一个值是空的

[
{
"id": 1,
"continent": "North America",
"country": "United States"
},
{
"id": 2,
"continent": "Europe",
"country": "Germany"
},
{
"id": 3,
"continent": "Asia",
"country": null
}
]

情况2:“国家”之一没有值(value)
[
{
"id": 1,
"continent": "North America",
"country": "United States"
},
{
"id": 2,
"continent": "Europe",
"country": "Germany"
},
{
"id": 3,
"continent": "Asia"
}
]

我使用 TableRowTable中显示“大陆”和“国家”,
TableRow(children: [
TableCell(child: Text(continent.continent)),
TableCell(child: Text(continent.country))])

但是,如果List或 continent.country == null中没有“国家” =>我不想显示该 TableRow,那么请帮助我设置以下条件:
  • 案例1
  • 案例2
  • 案例1 +案例2

  • 这是主文件:
    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))]),
    ]),
    ),
    ],
    ),
    );
    }
    }

    最佳答案

    您应该将大洲重命名为“区域”或“区域”,并且可以使用简单的if条件:

    (...)
    Table(children: [
    for (Zone zone in zones)
    If (zone.country != null )
    TableRow(children[
    TableCell(child: Text(zone.continent)),
    TableCell(child: Text(zone.country)),
    ])
    )

    关于flutter - Flutter:如果值为空或没有值,如何隐藏TableRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62240092/

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