Flutter项目转V2 embedding后编译失败FlatButton未定义报错
问题描述
运行GitHub上的flutter_alarm_clock时钟项目(项目地址:https://github.com/afzalali15/flutter_alarm_clock)时,最初构建提示需要Convert to V2 embedding,按官方迁移步骤完成配置更新后,再次构建出现报错无法编译,报错日志如下:
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode... Running Gradle task 'assembleDebug'... Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/03 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/03 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/03 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: unexpected element (uri:"", local:"extension-level"). Expected elements are <{}codename>,<{}layoutlib>,<{}api-level> Warning: unexpected element (uri:"", local:"base-extension"). Expected elements are <{}codename>,<{}layoutlib>,<{}api-level> lib/views/homepage.dart:65:16: Error: The method 'FlatButton' isn't defined for the class '_HomePageState'. - '_HomePageState' is from 'package:clock_app/views/homepage.dart' ('lib/views/homepage.dart'). Try correcting the name to the name of an existing method, or defining a method named 'FlatButton'. return FlatButton( ^^^^^^^^^^ lib/views/alarm_page.dart:157:36: Error: The method 'FlatButton' isn't defined for the class '_AlarmPageState'. - '_AlarmPageState' is from 'package:clock_app/views/alarm_page.dart' ('lib/views/alarm_page.dart'). Try correcting the name to the name of an existing method, or defining a method named 'FlatButton'. child: FlatButton( ^^^^^^^^^^ lib/views/alarm_page.dart:179:47: Error: The method 'FlatButton' isn't defined for the class '_AlarmPageState'. - '_AlarmPageState' is from 'package:clock_app/views/alarm_page.dart' ('lib/views/alarm_page.dart'). Try correcting the name to the name of an existing method, or defining a method named 'FlatButton'. FlatButton( ^^^^^^^^^^ FAILURE: Build failed with an exception. * Where: Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1159 * What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'. > Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1 * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 36s Exception: Gradle task assembleDebug failed with exit code 1
故障原因
- 日志开头的namespace映射、unexpected element警告是旧版Gradle插件和本地新版Android SDK不兼容导致的,不会直接造成构建失败,可以暂时忽略。
- 构建失败的核心原因是代码中使用的
FlatButton组件不存在:该项目基于早期Flutter版本开发,FlatButton是旧版本Material组件,Flutter 2.0及之后的版本已经正式废弃并移除了这个组件,替换为TextButton,本地高版本Flutter无法识别旧组件名就会抛出编译错误。
解决方法
- 替换废弃组件
找到项目中3处使用FlatButton的位置,直接将组件名替换为TextButton即可,两者参数完全兼容,不需要修改其他属性配置。对应文件位置:
lib/views/homepage.dart第65行lib/views/alarm_page.dart第157行lib/views/alarm_page.dart第179行
- 清理构建缓存
在项目根目录执行以下命令,清除旧的构建缓存并重新拉取依赖:
flutter clean flutter pub get
- 重新启动项目即可正常编译运行。
如果需要消除开头的Android相关警告,可以把android目录下build.gradle中的Gradle插件版本升级到和本地Flutter版本匹配的版本,不升级也不会影响项目功能正常运行。
内容的提问来源于stack exchange,提问作者Vinay Hegde
相关产品推荐
相关产品推荐

