如何显示我的List< Object>进入Listview? [英] How to display my List<Object> into Listview?

查看:87
本文介绍了如何显示我的List< Object>进入Listview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Json文件作为资产。解析工作完美,但是当我使用json中的列表时,会引发错误。 该吸气剂被调用为空

i have an Json File as assets. parsing works perfect, but when i use the list from my json, an error is thrown. "the getter was called on null"

这是我的JsonCode:

Here my JsonCode:

class Biersorten{
  final List<Bier> biersorten;

  Biersorten({
    this.biersorten
  });

  factory Biersorten.fromJson(List<dynamic> parsedJson){
    List<Bier> listBiersorten = new List<Bier>();
    listBiersorten = parsedJson.map((i)=>Bier.fromJson(i)).toList();

    return new Biersorten(
      biersorten: listBiersorten,
    );
  }
}

class Bier{
  int id;
  String name;
  String firmenname;
  String alkoholgehalt;

  Bier({this.id, this.name, this.firmenname, this.alkoholgehalt});

  factory Bier.fromJson(Map<String, dynamic> parsedJson){
    return Bier(
        id: parsedJson["id"],
        name : parsedJson["name"],
        firmenname : parsedJson ["firmenname"],
        alkoholgehalt: parsedJson["alkoholgehalt"]
    );
  }
}

这里是我的HomePageCode:

Here my HomePageCode:

class MyHomePage extends StatelessWidget {
  final String title;
  Biersorten biersorten;

  MyHomePage({this.title}){
    loadBier();
  }

  Future<String> _loadBierAsset() async {
    return await rootBundle.loadString("assets/JSON/Beer.json");
  }

  Future loadBier() async {
    String jsonString = await _loadBierAsset();
    final jsonResponse = json.decode(jsonString);
    biersorten = new Biersorten.fromJson(jsonResponse);
    print(biersorten.biersorten[0].alkoholgehalt);
  }

  Widget getCard(String title){
    return Card(
        child: Container(
          height: 50,
          child: Center(
            child: Text(title),
          ),
        )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Column(
        children: <Widget>[
          ListView.builder(
            itemCount: biersorten.biersorten.length,
            itemBuilder: (context, i){
              return getCard(biersorten.biersorten[i].name);
            },
          )
        ],
      ),
    );
  }
}

在函数 _loadBierAsset( )我可以使用列表,但是在ListView.builder中会引发错误。
我怎么解决这个问题?

In the Function _loadBierAsset() i can use the List, but in the ListView.builder it throw an error. How can i solve this problem?

感谢帮助:)

推荐答案

这是因为您的函数是异步的,并且在构建列表之前不会返回。直到该列表尚未初始化。首先将您的小部件从无状态更改为有状态。然后执行以下操作:

It is because your function is async and it doesn't return before the list is built. Till that time the list hasn't been initialized.First change your widget from Stateless to Stateful. Then Do this:

    body: biersorten!=null ?
        Column(
            children: <Widget>[
              ListView.builder(
                itemCount: biersorten.biersorten.length,
                itemBuilder: (context, i){
                  return getCard(biersorten.biersorten[i].name);
                },
              )
            ],
          ): Center(child: CircularProgressIndicator()),

并将您的loadBier函数更改为

And change your loadBier function to

Future loadBier() async {
    String jsonString = await _loadBierAsset();
    final jsonResponse = json.decode(jsonString);
    setState((){
    biersorten = new Biersorten.fromJson(jsonResponse);
    });
    print(biersorten.biersorten[0].alkoholgehalt);
  }

然后在您的initState()中添加

And in your initState() add

loadBier();

这篇关于如何显示我的List&lt; Object&gt;进入Listview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆