我试图在循环中传递一个列表来检索字符串并将它们显示在按钮中,这正在发生:
final List<Map<String, Object>> perguntas = [
{
'texto': 'Quantos anos o Britto tem?',
'Respostas': ['22', '21', '23'],
},
{
'texto': 'Britto é viado?',
'Respostas': ['Sim', 'Não', 'Sim e não'],
},
{
'texto': 'Qual o jogo favorito do Britto?',
'Respostas': ['Dark souls', 'Elden Ring', 'Assassins Creed', 'Trove'],
},
{
'texto': 'De onde o britto é?',
'Respostas': ['Tangamandápio', 'Favela do rio', 'Alphaville', 'SP'],
}
];
var respostas = [];
for (String textresposta in perguntas[question_changing]['Respostas'].cast()) {
respostas.add(textresposta);
}`
它给了我:
The method cast can't be unconditionally invoked because the receiver can be 'null'
所以我这样做:
for (String textresposta in perguntas[question_changing]['Respostas']!.cast())
它给了我:
The method cast isn't defined for the type Object, try correcting the name to the name of an existing method, or defining the method cast
我尝试将映射传递给循环,代码显示它为空,并且我无法使用 NullSafety,因为强制转换无法使用它来转换变量。
如果删除强制转换,则会收到类型错误:“‘for’循环中使用的类型‘Object’必须实现‘Iterable’。
尝试将
.cast()
重写为 as List
final List<Map<String, Object>> perguntas = [
{
'texto': 'Quantos anos o Britto tem?',
'Respostas': ['22', '21', '23'],
},
{
'texto': 'Britto é viado?',
'Respostas': ['Sim', 'Não', 'Sim e não'],
},
{
'texto': 'Qual o jogo favorito do Britto?',
'Respostas': ['Dark souls', 'Elden Ring', 'Assassins Creed', 'Trove'],
},
{
'texto': 'De onde o britto é?',
'Respostas': ['Tangamandápio', 'Favela do rio', 'Alphaville', 'SP'],
}
];
var respostas = [];
for (String textresposta in (perguntas[question_changing]['Respostas'] as List)) {
respostas.add(textresposta);
}
print(respostas);