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

Flutter AppBar调试报错:调用的构造函数非const构造函数

解决Flutter中"The constructor being called isn't a const constructor"错误

问题根源在于你给MaterialApp添加了const修饰符,但它的home参数对应的Scaffold并不支持const构造函数。当使用const修饰组件时,其所有子属性必须同样是可被const初始化的类型,因此触发了这个错误。

修改后的代码

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp( // 移除此处的const修饰符
      title: 'Bitcoin Weather',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: Text('Test')),
      ),
    );
  }
}

可选性能优化

如果想尽可能利用const优化组件性能,可以给支持const构造的子组件加上const修饰(比如AppBar和Text),但核心还是要移除MaterialApp的const:

home: Scaffold(
  appBar: const AppBar(title: const Text('Test')),
),

内容的提问来源于stack exchange,提问作者Alain Okala

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 01:14:59