简体   繁体   English

Flutter/Dart:创建/分配动态列表

[英]Flutter/Dart: Create/assign dynamic List

A list "options" is generated from json file as below and no issue: For simplicity, some parts were removed列表“选项”是从 json 文件生成的,如下所示,没有问题:为简单起见,删除了一些部分

Future getidea(String subject) async {

  List ideaList = [];
  for (var idea in ideaListTemp) {
    List options = [];
    options.add(idea["A"]);
    options.add(idea["B"]);
    options.add(idea["C"]);

    ideaList.add(ideaItem(
      idea["ideaText"],
      idea["ideaMedia"],
      idea[options],
    ));
  }
  return (ideaList);
}

class ideaItem {
  final String ideaText;
  final String ideaMedia;
  List? options;

  ideaItem(
    this.ideaText,
    this.ideaMedia,
    this.options,
  );
}

However, when I use "options" to create a Widget, this error occur.但是,当我使用“选项”创建一个 Widget 时,会发生此错误。 error: The argument type 'List?'错误:参数类型“列表?” can't be assigned to the parameter type 'List'.不能分配给参数类型“列表”。

  Widget build(BuildContext context) {
    return Expanded(
      child: SingleChildScrollView(
        padding: const EdgeInsets.all(_horizontalMargin),
        physics: const AlwaysScrollableScrollPhysics(),
        child: Column(
          children: [
            _buildOptions(widget.fromJSON.options), // this will have the error message
          ],
        ),
      ),
    );
  }
  
  Widget _buildOptions(List option) {
  List<Widget> listElevatedButton = [];
  for (var i = 0; i < option.length; i++) {
    //print(allOptions[i]);
    listElevatedButton.add(
      Builder(
        builder: (context) => Padding(
          padding: const EdgeInsets.all(8.0),
          child: ElevatedButton(
            onPressed: () {
              print('Use the update function');
            },
            child: Text(
              option[i],
            ),
          ),
        ),
      ),
    );
  }
  return Column(children: listElevatedButton);
}

What is the best practice to solve the issue?解决问题的最佳做法是什么?

The variable options is being used carelessly, You need to make sure that the options has value before using it, To do this you can put a null check like so:不小心使用了变量选项,您需要确保options在使用之前具有值,为此您可以像这样进行 null 检查:

_buildOptions(widget.fromJSON.options) -> _buildOptions(widget.fromJSON.options?? []) _buildOptions(widget.fromJSON.options) -> _buildOptions(widget.fromJSON.options?? [])

This will give an empty list to _buildOptions function as long as options is empty and It won't build any widgets in _buildOptions function.只要options为空,这将为_buildOptions function 提供一个空列表,并且它不会在_buildOptions function 中构建任何小部件。

I hope you understand the concept here.我希望你理解这里的概念。

Your properties 'options' has type (optional)?List, which mean it can contain List or null value.您的属性“选项”具有类型(可选)?列表,这意味着它可以包含列表或 null 值。 And you try use 'options' like argument in '_buildOptions' function, which need type List, not (optional)?List.你尝试在'_buildOptions' function 中使用'options'之类的参数,它需要类型列表,而不是(可选)?列表。

Rewrite code like this:像这样重写代码:

Column(
  children: [
    _buildOptions(widget.fromJSON.options ?? []),
  ],
),

If the "widget.fromJSON.options" value is null, the _buildOptions will have the empty List, otherwise, it will have the value of the "widget.fromJSON.options" value.如果“widget.fromJSON.options”值为 null,则 _buildOptions 将具有空列表,否则,它将具有“widget.fromJSON.options”值的值。

Good luck;)祝你好运;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM