1

你好我有一个简单的问题,我在这里做错了什么?我正在尝试在 Scaffold 中创建一个 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.')
      ),
    );
  }
}

这是输出的错误:

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

新错误:

../../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)隔离指令=0,vm_instructions=7ff65bad4f10 pc 0x00007ff65bcdaa42 fp 0x00000056bb8ff3c0 Dart_IsPrecompiledRuntime+0x21a352 -- DumpStackTrace 结束

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

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

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

Process 'command 'C:\Users\A\Documents\flutter\bin\flutter.bat'' 以非零退出值 -1073740791 结束

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

1 回答 1

1

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.')
      ),
    );
  }
}
于 2021-12-22T21:47:25.243 回答