You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter报错咨询:main.dart文件提示‘Expected to find ','’

解决Flutter main.dart中“Expected to find ','”报错

这个报错是Dart语法错误,核心原因是代码里需要用逗号分隔的位置漏掉了逗号,常见场景包括:

  • Widget的多个属性之间(比如appBar:和body:结尾)
  • Row/Column等组件的children列表里的元素末尾

从你提供的截图代码来看,问题出在MaterialApp的home字段下的Scaffold代码结构里,大概率是属性或子组件末尾缺了逗号。

快速解决方法

  1. 手动补全逗号:检查所有Widget属性和children列表里的元素,确保每个条目末尾都加上逗号(Dart允许末尾逗号,建议都加上,避免这类错误)
  2. 用IDE自动格式化:在Android Studio/VSCode里按下快捷键(Windows/Linux是Ctrl+Alt+L,Mac是Cmd+Option+L),IDE会自动修复这类语法格式问题

正确代码示例

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: const Scaffold(
        appBar: AppBar(title: Text('测试页面')),
        body: Center(child: Text('Hello Flutter')),
      ),
    );
  }
}

内容的提问来源于stack exchange,提问作者Afaq Ahmad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 17:01:21