您好,我有一個簡單的問題,我在這里做錯了什么?我正在嘗試在 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”的建構式或工廠。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),(isolate000000)(isolate000000) isolate_instructions=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' 執行失敗。
行程 'command'C:\Users\A\Documents\flutter\bin\flutter.bat'' 以非零退出值結束 -1073740791
- 嘗試:使用 --stacktrace 選項運行以獲取堆疊跟蹤。使用 --info 或 --debug 選項運行以獲得更多日志輸出。使用 --scan 運行以獲得完整的見解。
uj5u.com熱心網友回復:
只需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.')
),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391691.html
