Flutter:如何在 ListView.builder 中显示来自 ListView.builder 的数组数据?

标签 flutter dart

我有一个这样的 JSON 文件:

[
  {
    "id": 1,
    "continent": "North America",
    "country": [
      {
        "name": "United States",
        "capital": "Washington, D.C.",
        "language": [
          "English"
        ]
      },
      {
        "name": "Canada",
        "capital": "Ottawa",
        "language": [
          "English",
          "French"
        ]
      }
    ]
  },
  {
    "id": 2,
    "continent": "Europe",
    "country": [
      {
        "name": "Germany",
        "capital": "Berlin",
        "language": [
          "German"
        ]
      }
    ]
  }
]

我用过 https://app.quicktype.io/解析 JSON 数据。
我创建了一个 ListView.builder显示大洲的名称。 现在我想显示每个大陆的“国家名称”、“首都”和“语言”。

enter image description here

所以请帮我做到这一点,这是主文件
import 'package:flutter/material.dart';
import 'model/continent_model.dart';
import 'services/continent_services.dart';

class ContinentPage extends StatefulWidget {
  ContinentPage() : super();
  @override
  _ContinentPageState createState() => _ContinentPageState();
}

class _ContinentPageState extends State<ContinentPage> {
  List<Continent> _continent;
  List<Country> _country;

  @override
  void initState() {
    super.initState();
    ContinentServices.getContinent().then((continents) {
      setState(() {
        _continent = continents;
      });
    });
  }

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(),
        body: Container(
            child: ListView.builder(
                itemCount: null == _continent ? 0 : _continent.length,
                itemBuilder: (context, index) {
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Text(_continent[index].continent),
                      // how to create a ListView show a Column that includes:
                      // _country[index].name,
                      // _country[index].capital,
                      // _country[index].language,
                    ],
                  );
                })));
  }
}

最佳答案

您需要将第二个 listview.builder 放置在具有定义高度的容器内。

Container(
  height: 120,
  child: ListView.builder(
    shrinkWrap: true,
    scrollDirection: Axis.horizontal,
    itemCount: map.length,
    itemBuilder: (context, index) => Column(
      children: [
        Text(_country.elementAt(index).name),
        Text(_country.elementAt(index).capital),
Text(_country.elementAt(index).language),
      ],
    ),
  ),
)

关于Flutter:如何在 ListView.builder 中显示来自 ListView.builder 的数组数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62185529/

相关文章:

asynchronous - Dart编程语言的异步功能无法正常工作

singleton - Dart 中的 Controller 是单例吗

flutter - 如何修复八边形FloatingActionButton错误?

flutter - AnimatedSize 裁剪小部件

firebase - 先 flutter 不响应的地方(firestore)

android - Flutter- 在模拟器上运行但不在设备上运行的应用程序(使用 http 包)

flutter - 如何在 Flutter 中切换到暗/亮模式按钮?

flutter - StatelessWidget中的构建函数不断刷新

flutter - 在提供者之间共享数据

asynchronous - 在Dart中以同步方式调用异步功能