dart 添加动态表行值列表

xuo3flqw  于 2023-11-14  发布在  其他
关注(0)|答案(4)|浏览(117)

我尝试添加动态表行列表值,但当我单击Add Box添加表列表时,它显示此错误。
Another exception was thrown: type 'List<Map<String, Object>>' is not a subtype of type 'Map<List<dynamic>, dynamic>' in type cast的值。“

Container(
            // add button
            margin: EdgeInsets.only(top: 10),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(2.0),
                color: Colors.grey[200],
                border: Border.all(color: Colors.grey[500], width: 1.5)),
            height: data.size.width * 0.039,
            child: FlatButton(
                onPressed: () {
                  setState(() {
                    Map<List, dynamic> rowValue = Map<List, dynamic>();
                    rowValue = [
                      {
                        'serialNo': '',
                        "table": [
                          [
                            {'first_a': ''},
                            {'Second_a': ''},
                            {'Third_a': ''},
                            {'Fourth_a': ''},
                            {'Fifth_a': ''},
                            {'Sixth_a': ''},
                            {'Seventh_a': ''},
                            {'Eighth_a': ''}
                          ],
                          [
                            {'first_b': ''},
                            {'Second_b': ''},
                            {'Third_b': ''},
                            {'Fourth_b': ''},
                            {'Fifth_b': ''},
                            {'Sixth_b': ''},
                            {'Seventh_b': ''},
                            {'Eighth_b': ''}
                          ],
                          [
                            {'first_c': ''},
                            {'Second_c': ''},
                            {'Third_c': ''},
                            {'Fourth_c': ''},
                            {'Fifth_c': ''},
                            {'Sixth_c': ''},
                            {'Seventh_c': ''},
                            {'Eighth_c': ''}
                          ],
                          [
                            {'first_d': ''},
                            {'Second_d': ''},
                            {'Third_d': ''},
                            {'Fourth_d': ''},
                            {'Fifth_d': ''},
                            {'Sixth_d': ''},
                            {'Seventh_d': ''},
                            {'Eighth_d': ''}
                          ],
                        ]
                      }
                    ] as Map<List, dynamic>;
                    print("get value new");
                    print(rowValue);
                    this.rowList.add(rowValue);
                    // updateDetails();
                  });
                },
                child: Text('+ Add Box',
                    style: TextStyle(
                        color: Colors.grey[900],
                        fontWeight: FontWeight.bold,
                        fontSize: data.size.width * 0.019))),
          ),
        ]));

字符串
这是我在dartPad中的完整代码
每个表格单元格用户都可以插入数据。这个问题从这个make model of array object in flutter继续

pb3s4cty

pb3s4cty1#

声明的类型和分配的数据类型不同。声明的类型为Map<List, dynamic>,分配的数据为List<Map<String, Object>>
由于rowValue数据列表中只有一个map,我们可以删除列表类型和强制转换,因为我们不需要强制转换类型。
这应该能行

Map<String, dynamic> rowValue = <String, dynamic>{};

            rowValue = {
              'serialNo': '',
              "table": [
                [
                  {'first_a': ''},
                  {'Second_a': ''},
                  {'Third_a': ''},
                  {'Fourth_a': ''},
                  {'Fifth_a': ''},
                  {'Sixth_a': ''},
                  {'Seventh_a': ''},
                  {'Eighth_a': ''}
                ],
                [
                  {'first_b': ''},
                  {'Second_b': ''},
                  {'Third_b': ''},
                  {'Fourth_b': ''},
                  {'Fifth_b': ''},
                  {'Sixth_b': ''},
                  {'Seventh_b': ''},
                  {'Eighth_b': ''}
                ],
                [
                  {'first_c': ''},
                  {'Second_c': ''},
                  {'Third_c': ''},
                  {'Fourth_c': ''},
                  {'Fifth_c': ''},
                  {'Sixth_c': ''},
                  {'Seventh_c': ''},
                  {'Eighth_c': ''}
                ],
                [
                  {'first_d': ''},
                  {'Second_d': ''},
                  {'Third_d': ''},
                  {'Fourth_d': ''},
                  {'Fifth_d': ''},
                  {'Sixth_d': ''},
                  {'Seventh_d': ''},
                  {'Eighth_d': ''}
                ],
              ]
            };

          
            this.rowList.add(rowValue);

字符串

gudnpqoy

gudnpqoy2#

您的数据和此参数的类型不匹配。从您的代码示例中,我将关注rowValue的值,我假设您设置了错误的类型。尝试将rowValue的类型更改为List<Map<String,dynamic>> rowValue

elcex8rz

elcex8rz3#

rowValue的声明类型为Map<List,dynamic>,但下面的值的类型为List您可以修改代码

Map<List, dynamic> rowValue = Map<List, dynamic>();
    rowValue = {
  'serialNo': '',
  "table": [
    [
      {'first_a': ''},
      {'Second_a': ''},
      {'Third_a': ''},
      {'Fourth_a': ''},
      {'Fifth_a': ''},
      {'Sixth_a': ''},
      {'Seventh_a': ''},
      {'Eighth_a': ''}
    ],
    [
      {'first_b': ''},
      {'Second_b': ''},
      {'Third_b': ''},
      {'Fourth_b': ''},
      {'Fifth_b': ''},
      {'Sixth_b': ''},
      {'Seventh_b': ''},
      {'Eighth_b': ''}
    ],
    [
      {'first_c': ''},
      {'Second_c': ''},
      {'Third_c': ''},
      {'Fourth_c': ''},
      {'Fifth_c': ''},
      {'Sixth_c': ''},
      {'Seventh_c': ''},
      {'Eighth_c': ''}
    ],
    [
      {'first_d': ''},
      {'Second_d': ''},
      {'Third_d': ''},
      {'Fourth_d': ''},
      {'Fifth_d': ''},
      {'Sixth_d': ''},
      {'Seventh_d': ''},
      {'Eighth_d': ''}
    ],
  ]
};

字符串

rjee0c15

rjee0c154#

这里的问题是因为当你试图将List<Map<String,Object>>分配给Map<List,dynamic>时,类型不匹配。
看起来你想添加一个行列表到你的rowList中,每一行都被表示为一个map,其中包含一个map列表的列表。你可以修改代码如下:
这看起来是有效的

onPressed: () {
  setState(() {
    Map<String, dynamic> rowValue = {
      'serialNo': '',
      'table': [
        [
          {'first_a': ''},
          {'Second_a': ''},
          {'Third_a': ''},
          {'Fourth_a': ''},
          {'Fifth_a': ''},
          {'Sixth_a': ''},
          {'Seventh_a': ''},
          {'Eighth_a': ''}
        ],
        [
          {'first_b': ''},
          {'Second_b': ''},
          {'Third_b': ''},
          {'Fourth_b': ''},
          {'Fifth_b': ''},
          {'Sixth_b': ''},
          {'Seventh_b': ''},
          {'Eighth_b': ''}
        ],
        // ... similar rows ...
      ]
    };

    this.rowList.add(rowValue);
  });
},

字符串

相关问题