flutter 在列表中添加多个列表- dart

bq9c1y66  于 2023-03-19  发布在  Flutter
关注(0)|答案(2)|浏览(219)

我有这个密码

var sections = [];
    
    
    sections.add({'name': 'test', 'qustions' : []});
    sections[0]['qustions'] = {'asd':'qustions 1'};
    sections[0]['qustions'] = {'name':'qustions 2'};

    print(sections);

退货
[{name: test, qustions: {name: qustions 2}}]
我希望它返回类似于
[{name: test, qustions: [{name: qustions 1},{name: qustions 2}]}]

qxgroojn

qxgroojn1#

每次使用map[key]=newValue时,它都会重置那个键的值。这里需要的是对list执行add

var sections = [];

sections.add({'name': 'test', 'qustions': []});

sections[0]['qustions'].add({'asd': 'qustions 1'});
sections[0]['qustions'].add({'name': 'qustions 2'});
print(sections);

您可能还想创建模型类。

omvjsjqw

omvjsjqw2#

可以使用insert方法:

var sections = [];

  sections.add({'name': 'test', 'qustions': []});
  sections[0]['qustions'] = {'name': 'qustions 1'};
  sections.insert(1, {'name': 'qustions 2'});

输出

[{name: test, qustions: [{name: qustions 1},{name: qustions 2}]}]

相关问题