Flutter代码提示Expected to find ']',tolist()所在行报错求解
问题原因及修复方案
你的代码存在两个语法错误,对应修复方法如下:
- 括号配对逻辑错误
你错误地将转列表方法写在了Column组件的children数组闭合标记的外侧,逻辑变成了给Column组件调用转列表方法,完全不符合Dart语法要求。正确逻辑是转列表操作需要跟在answers数组的map方法之后,用来把map返回的可迭代对象转为Widget列表,方便展开运算符进行展开。 - 方法名拼写错误
Dart语法中可迭代对象转列表的方法为驼峰命名的toList(),你写的全小写tolist()是不存在的方法,会触发运行时报错。
修正后的对应代码段
@override Widget build(BuildContext context) { var questions = [ { 'questionText': 'What is your favourite color', 'answers': ['Black', 'Red', 'Green', 'White'] }, { 'questionText': 'What is your favourite Animal', 'answers': ['Tiger', 'Bull', 'Cat', 'Dog'] }, ]; return MaterialApp( home: Scaffold( appBar: AppBar( title: Text('My First App'), ), body: Column(children: [ Question(questions[_questionindex]['questionText']?.toString() ?? '', ), ...(questions[_questionindex]['answers'] as List<String>).map((answer) => Answer(answerques, answer)).toList() ]), ), ); }
内容的提问来源于stack exchange,提问作者kavya bhargava
相关产品推荐
相关产品推荐

