Flutter开发常见问题,解决小技巧

2024-09-03 12:38

本文主要是介绍Flutter开发常见问题,解决小技巧,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.Android Studio 4.0 以上打包编译报错;

Execution failed for task ':app:lintVitalRelease'.
> Could not resolve all artifacts for configuration ':app:debugRuntimeClasspath'.> Failed to transform libs.jar to match attributes {artifactType=processed-jar, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.> Execution failed for JetifyTransform: E:\FlutterSpace\xxxx\build\app\intermediates\flutter\debug\libs.jar.> Transform's input file does not exist: E:\FlutterSpace\xxxx\build\app\intermediates\flutter\debug\libs.jar. (See https://issuetracker.google.com/
issues/158753935)* 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

1.在子build.gradle添加以下代码:

android{lintOptions { checkReleaseBuilds false }
}

解决方案:原文地址:https://my.oschina.net/u/4364921/blog/4725237

2.Exception: Gradle build failed to produce an .apk file. It’s likely that this file was generated under /build, but the tool couldn’t find it.

打包时找不到渠道包名导致;

1.通过VSCode找到项目当中有一 launch.json 文件

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "YOUR_PROJECT_NAME",//你项目的名称"program": "lib/main.dart","request": "launch","type": "dart","args": ["--flavor","ADD_YOUR_FLAVOR_NAME_HERE" //配置你的渠道包名]}]

2.通过Andorid Studio 找到子build.gradle文件添加渠道配置;

android {compileOptions {targetCompatibility JavaVersion.VERSION_1_8sourceCompatibility JavaVersion.VERSION_1_8}android.applicationVariants.all {variant ->variant.outputs.all {//在这里修改apk文件名outputFileName = "-v${variant.versionName}-${variant.productFlavors[0].name}-release.apk"}}productFlavors {pgyer {}   // 蒲公英huawei {}       // 华为商店baidu {}        // 百度手机助手yinyongbao {}   // 应用宝xiaomi {}   // 小米oppo {}   // OPPOvivo {}   // VIVOs360 {}   // 360lenovo {}   // 联想aliuc {}   // 阿里UC}// 批量渠道包值替换(有些第三方库中需要用到渠道名)productFlavors.all { flavor ->// 友盟推送渠道包, UMENG_CHANNEL_VALUE 是根据你AndroidManifest.xml来配置的,请看下面。flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]}//解决打包异常的问题lintOptions { checkReleaseBuilds false }
}

解决方案:https://stackoverflow.com/questions/59732883/flutter-gradle-build-failed-to-produce-an-apk-file-its-likely-that-this-file

3._positions.isNotEmpty, ‘ScrollController not attached to any scroll views.’

具体报错问题;

The following assertion was thrown building Swiper(state: _SwiperState#e86c5):
ScrollController not attached to any scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 108 pos 12: '_positions.isNotEmpty'

解决方案:https://blog.csdn.net/youtiankeng/article/details/107419322

4.Android studio Flutter 开发快捷键

https://flutter.cn/docs/development/tools/android-studio

5.Flutter WebView js交互

html 测试代码块

<html>
<head><title>Js调用Flutter</title><script>function Close() {AppWndClose.postMessage("JS调用了Flutter");}</script>
</head><body>
<br>
<input type="button" value="关闭" onclick="Close()"/>
</body>
</html>

flutter 测试代码块

WebView(javascriptChannels: <JavascriptChannel>[ JavascriptChannel(name: 'AppWndClose',onMessageReceived: (JavascriptMessage message) {showToast(message.message);})].toSet(),
)

我们要注意的是代码里面使用的都是AppWndClose ;这个是我们要注意到的;
参考地址:https://zhuanlan.zhihu.com/p/124797055

6.Flutter WebView跳转dart界面

  1. 通过js的方法进行调用;
class CustomWebViewPage extends StatefulWidget {final String url;CustomWebViewPage({Key key,@required this.url,});@overridecreateState() => _PageState();
}class _PageState extends State<CustomWebViewPage> with WidgetsBindingObserver {_PageState();FlutterWebviewPlugin _webViewPlugin = FlutterWebviewPlugin();double lineProgress = 0.0;bool loadError = false;HomeModel homeModel;initState() {super.initState();_webViewPlugin.onProgressChanged.listen((progress) {print(progress);setState(() {lineProgress = progress;});});_webViewPlugin.onHttpError.listen((progress) {setState(() {loadError = true;});});_webViewPlugin.onStateChanged.listen((event) {if (event.type == WebViewState.finishLoad) {}setState(() {});});WidgetsBinding.instance.addObserver(this);EventBus.getDefault().register(this, (data) {if (data is PublishPostsEvent || data == 'tabRankList') {_webViewPlugin.evalJavascript("RefreshMissionList()").then((res) {print("evaluateJavascript-res: ${res}");});}});_wlakPermission(false);}@overridevoid didChangeAppLifecycleState(AppLifecycleState state) {print("-didChangeAppLifecycleState-" + state.toString());switch (state) {case AppLifecycleState.inactive: // 处于这种状态的应用程序应该假设它们可能在任何时候暂停。break;case AppLifecycleState.resumed: //从后台切换前台,界面可见_webViewPlugin.evalJavascript("AppOnShow()").then((res) {print("evaluateJavascript-res: ${res}");});break;case AppLifecycleState.paused: // 界面不可见,后台_webViewPlugin.evalJavascript("AppOnHide()").then((res) {print("evaluateJavascript-res: ${res}");});;break;case AppLifecycleState.detached: // APP结束时调用break;}}@overrideWidget build(BuildContext context) {homeModel = Provider.of<HomeModel>(context);return WebviewScaffold(url: widget.url,mediaPlaybackRequiresUserGesture: false,withZoom: false,withJavascript: true,clearCookies: true,hidden: true,javascriptChannels: <JavascriptChannel>[JavascriptChannel(name: "AppWndClose",onMessageReceived: (JavascriptMessage message) {NavigatorUtils.goBack(context);}),_postThread(),_partInTopic(),].toSet(),initialChild: Container(child: Stack(children: [Center(child: StateLayout(type: loadError ? StateType.network : StateType.loading,)),_setTitle(context),],),));}_postThread() {return JavascriptChannel(name: "eventPostThread",onMessageReceived: (JavascriptMessage message) {_webViewPlugin.hide();print("参数: ${message.message}");NavigatorUtils.push(context, ProfileRouter.publishPosts).then((value) {_webViewPlugin.show();_webViewPlugin.evalJavascript("RefreshMissionList()").then((res) {print("evaluateJavascript-res: ${res}");});});});}_partInTopic() {return JavascriptChannel(name: "eventTaskPartinTopic",onMessageReceived: (JavascriptMessage message) {// 因为flutter_webview_plugin 是浮在flutterUI上的一层视图,它不在widget树中,跳转到dart页面时,需要隐藏掉web页面_webViewPlugin.hide();NavigatorUtils.push(context, TopicRouter.topic).then((value) {//再跳回来时再显示web页面_webViewPlugin.show();_webViewPlugin.evalJavascript("RefreshMissionList()").then((res) {//同时调用web端接口进行刷新操作;});});});}_setTitle(context) {return Positioned(left: 15,top: MediaQuery.of(context).padding.top + 5,child: BackButton(onPressed: () {NavigatorUtils.goBack(context);},),);}_progressBar(double progress, BuildContext context) {return Offstage(offstage: progress == 1.0,child: Container(child: LinearProgressIndicator(backgroundColor: Colors.blueAccent.withOpacity(0),value: progress == 1.0 ? 0 : progress,valueColor: new AlwaysStoppedAnimation<Color>(Colors.lightBlue),),height: 2,));}@overridevoid dispose() {_webViewPlugin.dispose();WidgetsBinding.instance.removeObserver(this);EventBus.getDefault().unregister(this);super.dispose();}
}
  1. 通过加载URL的形式进行监听响应跳转;

///省略上面代码;@overridevoid initState() {super.initState();flutterWebviewPlugin.onUrlChanged.listen((loadUrl){//WebviewScaffold 加载的url变化时触发此方法// print('===>>当前 url:$loadUrl');// h5 点退出按钮,关闭webViewif (loadUrl.endsWith('/backapp')){flutterWebviewPlugin.close();pop();}// 跳转到dart页面else if (loadUrl.endsWith('/policy')){// 因为flutter_webview_plugin 是浮在flutterUI上的一层视图,它不在widget树中,跳转到dart页面时,需要隐藏掉web页面,再跳回来时再显示web页面flutterWebviewPlugin.hide(); routePush(ClassRoomPage()).then((value) {flutterWebviewPlugin.show();flutterWebviewPlugin.reloadUrl(widget.url);});//跳转到dart页面时,也可以关闭掉web页面,再跳回来时再创建一个web页面// flutterWebviewPlugin.close();// routePush(ClassRoomPage()).then((value){//   flutterWebviewPlugin.launch(widget.url,//     rect: new Rect.fromLTWH( // 设置web页面的尺寸//       0.0,//       _navBarHeight,//       MediaQuery.of(_context).size.width,//       MediaQuery.of(_context).size.height-_navBarHeight,//     ),//   );// });}});//加载错误时监听// flutterWebviewPlugin.onHttpError.listen((error){//   print('加载错误:${error.code}  ${error.url}');// });//加载状态变化监听// flutterWebviewPlugin.onStateChanged.listen((state){//   print('状态监听:${state.type.toString()}');// });}

参考文章:flutter_webview_plugin 与dart页面跳转使用

7. cmdline-tools component is missing Run path/to/sdkmanager --install "cmdline-tools;latest"

参考文章:https://blog.csdn.net/weixin_41824429/article/details/118942087

8.android studio安装和flutter project创建卡住问题解决

参考文章:https://blog.csdn.net/lucynie/article/details/106928231

9. Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

参考文章:https://www.jianshu.com/p/4feeec038482

10.Flutter Getx 结合 TabView

参考文章:https://stackoverflow.com/questions/63924491/how-to-build-a-tabview-using-flutter-getx

11. Flutter 项目window应用鼠标无法滚动


const Set<PointerDeviceKind> _kTouchLikeDeviceTypes = <PointerDeviceKind>{PointerDeviceKind.touch,PointerDeviceKind.mouse,PointerDeviceKind.stylus,PointerDeviceKind.invertedStylus,PointerDeviceKind.unknown
};MaterialApp(...scrollBehavior: const MaterialScrollBehavior().copyWith(scrollbars: true,dragDevices: _kTouchLikeDeviceTypes),...
);//如果你使用的是Getx框架同上,有相同的属性;scrollBehavior

参考文章:https://www.cnblogs.com/yangyxd/p/15080537.html

12. Flutter 项目window应用运行异常

问题:

CMake Error at CMakeLists.txt:2 (project):GeneratorVisual Studio 16 2019could not find any instance of Visual Studio.

具体解决方法:

https://stackoverflow.com/questions/69944913/cmake-error-while-running-flutter-desktop-application/69951396#69951396 。 Flutter 2.9 版支持 VS 2022,而您使用的 2.8.1 版仅支持 VS 2019。只需安装 2019 并试一试。
如果你想在 2.8.1 版本中使用 VS 2022,这里是解决方法。 stackoverflow.com/a/69951396/6566310 我想这个解决方法是值得的。
感谢您提供这两种解决方案。但我确实选择了第一个,现在它正在工作。

解决方案:https://www.likecs.com/ask-5954170.html

12. Flutter创建本地 Library 工具类

解决方案:https://www.jianshu.com/p/70f874765965

13. Unsupported class file major version 59

解决方案:https://github.com/grpc/grpc-java/issues/8264

14. Lint infrastructure error

解决方案:https://juejin.cn/post/7037307560977760292

这篇关于Flutter开发常见问题,解决小技巧的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132979

相关文章

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

Goland debug失效详细解决步骤(合集)

《Golanddebug失效详细解决步骤(合集)》今天用Goland开发时,打断点,以debug方式运行,发现程序并没有断住,程序跳过了断点,直接运行结束,网上搜寻了大量文章,最后得以解决,特此在这... 目录Bug:Goland debug失效详细解决步骤【合集】情况一:Go或Goland架构不对情况二:

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

Java多线程父线程向子线程传值问题及解决

《Java多线程父线程向子线程传值问题及解决》文章总结了5种解决父子之间数据传递困扰的解决方案,包括ThreadLocal+TaskDecorator、UserUtils、CustomTaskDeco... 目录1 背景2 ThreadLocal+TaskDecorator3 RequestContextH

解决JavaWeb-file.isDirectory()遇到的坑问题

《解决JavaWeb-file.isDirectory()遇到的坑问题》JavaWeb开发中,使用`file.isDirectory()`判断路径是否为文件夹时,需要特别注意:该方法只能判断已存在的文... 目录Jahttp://www.chinasem.cnvaWeb-file.isDirectory()遇

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

SpringBoot中的404错误:原因、影响及解决策略

《SpringBoot中的404错误:原因、影响及解决策略》本文详细介绍了SpringBoot中404错误的出现原因、影响以及处理策略,404错误常见于URL路径错误、控制器配置问题、静态资源配置错误... 目录Spring Boot中的404错误:原因、影响及处理策略404错误的出现原因1. URL路径错

基于Python开发PPTX压缩工具

《基于Python开发PPTX压缩工具》在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,不便于传输和存储,所以本文将使用Python开发一个PPTX压缩工具,需要的可以了解下... 目录引言全部代码环境准备代码结构代码实现运行结果引言在日常办公中,PPT文件往往因为图片过大而导致文件体积过大,

MySQL报错sql_mode=only_full_group_by的问题解决

《MySQL报错sql_mode=only_full_group_by的问题解决》本文主要介绍了MySQL报错sql_mode=only_full_group_by的问题解决,文中通过示例代码介绍的非... 目录报错信息DataGrip 报错还原Navicat 报错还原报错原因解决方案查看当前 sql mo