真机调试 Flutter 报错:Lookup failed: title in @getters in MyHomePage in package:flutter_demo02/main.dart

发生缘由

学习 Flutter 更改 lib 目录下面的 main.dart 文件之后真机调试运行 flutter run 报错:

 1 # 小组件库异常
 2 ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
 3 # 构建MyHomePage的时候抛出下列_CompileTimeError
 4 The following _CompileTimeError was thrown building MyHomePage(dirty, dependencies:
 5 [_InheritedTheme, _LocalizationsScope-[GlobalKey#34833]], state: _MyHomePageState#2db8a):
 6 # 查找失败:包 MyHomePage 中 @getters 中的标题:flutter_demo02/main.dart
 7 Lookup failed: title in @getters in MyHomePage in package:flutter_demo02/main.dart
 8 
 9 # 相关的导致错误的小部件是:MyHomePage
10 The relevant error-causing widget was:
11   MyHomePage
12   MyHomePage:file:///D:/Prog/flutter/material/flutter_proj/flutter_demo02/lib/main.dart:34:19
13 
14 When the exception was thrown, this was the stack:
15 #0      StatefulElement.build (package:flutter/src/widgets/framework.dart:5583:27)
16 #1      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5471:15)
17 #2      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
18 #3      Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
19 #4      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2895:19)
20 #5      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:984:21)
21 #6      RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:457:5)
22 #7      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1325:15)
23 #8      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1255:9)
24 #9      SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:978:7)   
25 #13     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
26 (elided 3 frames from class _Timer and dart:async-patch)

main.dart 文件内容如下:

 1 import 'package:flutter/material.dart';
 2 
 3 void main() {
 4     // 顶层组件MaterialApp,它封装了应用程序实现 Material Design 所需要的一些 Widget。
 5     runApp(MaterialApp(
 6         // 基础布局组件Scaffold 是MaterialDesign布局结构的实现 提供了显示drawer、snackbar和底部sheet的API
 7         home: Scaffold(
 8             appBar: AppBar(title: const Text("你好Flutter")),
 9             // 抽离出来了一个HomeWidget的组件
10             body: const HomeWidget(),
11         ),
12     ));
13 }
14 
15 // 自定义组件其实就是一个类,这个类需要继承StatelessWidget或者StatefulWidget
16 // StatelessWidget 是无状态组件,状态不可变的widget
17 class HomeWidget extends StatelessWidget {
18     // 构造函数调用父类的构造函数
19     const HomeWidget({Key? key}) : super(key: key);
20 
21     @override
22     Widget build(BuildContext context) {
23         return const Center(
24             child: Text(
25                 "我是一个文本",
26                 textDirection: TextDirection.ltr,
27                 style: TextStyle(
28                     fontSize: 40.0,
29                     // color: Colors.yellow,
30                     color: Color.fromRGBO(244, 233, 121, 0.5),
31                 ),
32             ),
33         );
34     }
35 }

运行环境

  • 电脑系统版本:Windows 10 64bit

  • VS Code:1.72.0(user setup)

  • Flutter:3.16.0

# 查看Flutter版本
C:\Windows\System32>flutter --version
Flutter 3.16.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision db7ef5bf9f (4 weeks ago) • 2023-11-15 11:25:44 -0800
Engine • revision 74d16627b9
Tools • Dart 3.2.0 • DevTools 2.28.2

问题解决

问题出在 test 目录下面的 widget_test.dart 文件,正是它让项目构建失败了。因为修改完 main.dart 文件之后导致MyApp 组件,因此该测试文件就会出错。

解决原因很简单:

  • 将测试文件删掉

  • 亦或是将 HomeWidget 类更改为 MyApp。

最后告诫

删除掉文件亦或是修改完之后如果 r 键没有作用,那么可以 R 键。

常用快捷键作用
r 键 点击后热加载,也就算是重新加载吧
R键 热重启项目
p 键 显示网格,这个可以很好的掌握布局情况,工作中很有用
o 键 切换android和ios的预览模式
q 键 退出调试预览模式
posted @ 2023-12-10 16:51  炸天帮帮主  阅读(85)  评论(0编辑  收藏  举报