您好,我有一个简单的问题,我在这里做错了什么?我正在尝试在脚手架中创建一个 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 …