尝试在材质应用程序内使用文本时,AppBar 内出现 Flutter 非常量构造函数错误

Ash*_*Ash 3 dart flutter flutter-text

您好,我有一个简单的问题,我在这里做错了什么?我正在尝试在脚手架中创建一个 AppBar,但是当我尝试使用 Text 时,它似乎不起作用,并要求添加一个 Const,但是当我这样做时,它并不能解决问题。

抱歉,如果已经有这方面的信息,我只是不知道解决此问题所需查找的具体条款。我知道您可以将 AppBar 放在 void main() 中,但是我正在遵循教程,并且希望以类似的方式进行操作。

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('My First App'),
          ),
          body: Text('This is the body of text.')
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

这是输出的错误:

12:25:错误:无法调用需要 const 表达式的非“const”构造函数。尝试使用“const”的构造函数或工厂。appBar: const AppBar( ^^^^^^

新错误:

../../runtime/platform/allocation.cc:14:错误:内存不足。版本=2.14.4(稳定)(2021 年 10 月 13 日星期三 11:11:32 +0200)在“windows_x64”上 pid=24408,线程=30512,isolate_group=(nil)(0000000000000000),isolate=(nil)(0000000000000000) isolate_instructions=0, vm_instructions=7ff65bad4f10 pc 0x00007ff65bcdaa42 fp 0x00000056bb8ff3c0 Dart_IsPrecompiledRuntime+0x21a352 -- DumpStackTrace 结束

失败:构建失败并出现异常。

  • 其中:脚本'C:\Users\A\Documents\flutter\packages\flutter_tools\gradle\flutter.gradle'行:1005

  • 出了什么问题:任务“:app:compileFlutterBuildDebug”执行失败。

进程'命令'C:\ Users \ A \ Documents \ flutter \ bin \ flutter.bat''以非零退出值完成 -1073740791

  • 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获得更多日志输出。使用 --scan 运行以获得完整的见解。

Jah*_*lam 7

const只需在 Material 应用程序之前删除即可


import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('My First App'),
          ),
          body: Text('This is the body of text.')
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)