Flutter开发Bug总结

本篇文章总结一下在Flutter开发过程中遇到的BUG及其解决方案,不定期更新。

环境介绍

  • 系统:Windows 10 家庭中文版
  • 编译器:VS code
  • Android版本:Android8.1.0
  • Android SDK Tools: 26.1.1

flutter RangeError (index): Invalid value: Valid value range is empty: 0

  • 问题描述:在页面类中定义了一个List,然后在 initState() 中通过 add() 对其赋值,没有语法错误,但是在运行过程中报错。

  • 代码详情:

class _RootPage extends State<RootPage> {

int _currentIndex = 0;
List<Widget> pages = List();
@override
void initState() {
pages
..add(HomePage())
..add(ShopPage());
super.initState();
}

@override
Widget build(BuildContext context) {
// print(pages[0]);
return Scaffold(
body: pages[_currentIndex],

...

);
}
}
  • 错误详情:看报错是因为List中没有值,然后使用的时候就会出现引用错误。
I/flutter ( 3569): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 3569): The following RangeError was thrown building BottomAppBarNav(dirty, state:
I/flutter ( 3569): BottomAppBArNavState#0f3a1):
I/flutter ( 3569): RangeError (index): Invalid value: Valid value range is empty: 0
  • 解决方案:在项目根目录运行此命令:flutter clean,然后再次运行项目,正常运行。

  • 答案来源:GITHUB

  • 问题扩展:依据此issues,如果出现错误RangeError: RangeError (index): Invalid value: Only valid value is 0: 1,一样可以用此方法解决。

Failed assertion: line 5098 pos 14: ‘_dependents.isEmpty’: is not true.

class _QuotationDetailsStatePage extends State<QuotationDetailsPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('报价详情'),
bottom: TabBar(
tabs: [
Text('基本信息'),
Text('产品信息'),
Text('审批历史'),
],
controller: TabController(length: 3, vsync: ScrollableState(),),
),
),
body: SizedBox(height: 29,),
);
}
}
class _QuotationDetailsStatePage extends State<QuotationDetailsPage> with SingleTickerProviderStateMixin {

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('报价详情'),
bottom: TabBar(
tabs: [
Text('基本信息'),
Text('产品信息'),
Text('审批历史'),
],
controller: TabController(length: 3, vsync: this),
),
),
body: SizedBox(height: 29,),
);
}
}

Vertical viewport was given unbounded height.

ListView.separated(
itemBuilder: (context, index) =>
_buildGroupForm(index, _focusFieldList['fieldColumn'][index]),
separatorBuilder: (context, index) => SizedBox(
height: 2,
),
itemCount: _focusFieldList['fieldColumn'].length,
shrinkWrap: true, // 增加此句
);

键盘弹起底部溢出

Expanded(
child: ListView.sperated(...),
); // 用Expanded包住

RenderFlex children have non-zero flex but incoming height constraints are unbounded.

java.io.FileNotFoundException: /storage/emulated/0/Download/0039MnYb0qxYhV.mp3: open failed: EACCES (Permission denied)

<!-- android\app\src\main\AndroidManifest.xml -->
<application
android:requestLegacyExternalStorage="true"
...
>
...
</application>

参考

使用http请求/OS Error: Connection reset by peer, errno = 104

<!-- android\app\src\main\AndroidManifest.xml -->
<application
android:usesCleartextTraffic="true"
...
>
...
</application>

usesCleartextTraffic 表示允许所有明文请求。

动态更新dialog中的内容

参考

知识点

MediaQuery.of(context).padding 获取安全距离
MediaQuery.of(context).size 获取屏幕宽高

Tabbar页面保活
class ListDemoState extends State<ListDemo> with AutomaticKeepAliveClientMixin{
// AutomaticKeepAliveClientMixin 保存页面状态
@override
Widget build(BuildContext context) {
super.build(context);
return ListView.builder(itemBuilder: (context, index) => Text("内容$index"),itemCount: 100,);
}

@override
bool get wantKeepAlive => true;
}

java.lang.IllegalArgumentException: Failed to find configured root that contains

拍照报错

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 增加此句 -->
<root-path name="name" path="" />
</paths>

打包报错

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:lintVitalRelease'.
> Could not resolve all artifacts for configuration ':app:profileRuntimeClasspath'.
> 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:\web-flutter\project_business_flutter\build\app\intermediates\flutter\profile\libs.jar.
> Transform's input file does not exist: E:\web-flutter\project_business_flutter\build\app\intermediates\flutter\profile\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

BUILD FAILED in 17s

解决方法:

flutter clean
flutter build --profile
flutter build --debug
flutter build --release

Text selection index was clamped (-1->0) to remain in bounds. This may not be your fault, as some keyboards may select outside of bounds.

stackoverflow

onChanged: (value) {
textEditingController.value = textEditingController.value.copyWith(
text: value,
selection: TextSelection(baseOffset: value.length, extentOffset: value.length),
composing: TextRange.empty,
);
}

插件

url_launcher 打开url,其他app dart2.22以下需使用5.7.9或以前版本
intent 与原生通信 / android_intent
react_forms 表单验证
category_picker 类tabBar样式tab选择器
flutterfullscreenpicker 全屏选择
smart_select: ^4.3.2 选择器
multi_select_flutter: ^3.1.8 选择器
syncfusion_flutter_charts 图表
readmore 阅读更多
sticky_and_expandable_list 可折叠标题悬停列表
table_calendar 可操作定制日历
Flukit 插件库
flutter_slidable 左右滑动操作
Giffy Dialogs 动画弹框
syncfusion_flutter_calendar 付费日历插件
timetable 日历带时间表格
flutter_uploader 附件上传插件
flutter_screenutil 屏幕适配方案

文章作者: JaCo Wu
文章链接: https://jacokwu.cn/blog/2019/07/31/Flutter开发Bug总结/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 JaCo Wu的博客