Flutter使用过程遇到的问题总结

向阳而生

目录
1、 Flutter真机调试找不到设备:ERROR: Could not connect to lockdownd, error code -17
2、command not found: flutter
3、This class (or a class which this class inherits from) is marked as '@immutable', but one or more of its instance fields are not final: WebView.urldart(must_be_immutable)
4、Flutter本地图片不显示问题
5、Horizontal viewport was given unbounded height.
6、 _TypeError (type 'double' is not a subtype of type 'int')
7、WARNING: CocoaPods requires your terminal to be using UTF-8 encoding.
8、Failed assertion: line 319 pos 15: 'color == null || decoration == null'

1、Flutter真机调试找不到设备:ERROR: Could not connect to lockdownd, error code -17

解决方式:
修改var/db/lockdown文件夹的读写权限

步骤:
Finder ->前往菜单->前往文件夹->进入/var/db/

找到‘lockdown’ 文件夹 ->右键,显示简介->底部的‘共享与权限’ 项,打开右下角的锁(输入电脑密码)->加号,将自己的账号添加进权限表中,修改自己的权限为‘读与写’ ->锁定权限表即可。


再执行flutter doctor进行查看



zsh: command not found: flutter

图片.png

command not found: flutter这个错误,因为我们之前设置环境变量的时候,是直接在命令行通过export命令进行的,并没有全局设置。

  • 配置环境变量

如果没有~/.bash_profile文件不存在的问题,则新建

cd ~/
touch .bash_profile

打开.bash_profile文件:

open -e .bash_profile

在文件末尾设置追加设置flutter bin目录路径为环境变量:

export PATH=`pwd`/flutter/bin:$PATH

使配置立即生效

source ~/.bash_profile
图片.png
  • 全局配置

如上操作之后我们会发现如果我们关闭终端,重新打开VS Code,或者新开一个终端窗口,会发现 flutter 指令不能使用,我们需要将 flutter 的指令在任何窗口中生效。

1> 如果存在.zshrc文件
找到根目录下的 .zshrc 文件,使用文本编辑器打开,然后在最后面添加以下指令:

source ~/.bash_profile 

2> 如果不存在.zshrc文件

// 进入根目录
$ cd ~
// 打开/新建 .zshrc 文件
vim ~/.zshrc
//最后添加即可
source ~/.bash_profile 
图片.png

保存退出即可!
重启终端后,就可以在任意终端窗口中使用 flutter 指令。

*注意:
.zshrc文件是隐藏文件,command + shift+.即可显示与隐藏隐藏文件。

3、This class (or a class which this class inherits from) is marked as '@immutable', but one or more of its instance fields are not final: WebView.urldart(must_be_immutable)

class WebView extends StatefulWidget {

 //定义常量
  String url;
  final String statusColor;
  final String title;
  final bool hideAppBar;
  final bool backForbid;  
}

翻译过来大意:这个类(或者这个类继承的类)被标记为“@immutable”,但是它的一个或多个实例字段不是final:WebView.urldart(必须是不可变的)
所以在String url;前面加上final即可消除这个警告

4、Flutter本地图片不显示问题

Exception caught by image resource service
Unable to load asset: assets/images/type_travelgroup.png

图片.png

Flutter 中添加静态资源很简单,将静态资源放置在任意目录(通常是根目录下的 assets 文件夹中),然后在配置文件中 pubspec.yaml 中指定即可。每个 asset 都通过相对于 pubspec.yaml 文件所在位置的路径进行标识

  • 在pubspec.yaml 添加路径
  assets:
   - assets/images/
   - assets/json/
图片.png

flutter: _TravelPageState is a SingleTickerProviderStateMixin but multiple tickers were created.
A SingleTickerProviderStateMixin can only be used as a TickerProvider once.

flutter: _TravelPageState is a SingleTickerProviderStateMixin but multiple tickers were created.
A SingleTickerProviderStateMixin can only be used as a TickerProvider once.
If a State is used for multiple AnimationController objects, or if it is passed to other objects and those objects might use it more than one time in total, then instead of mixing in a SingleTickerProviderStateMixin, use a regular TickerProviderStateMixin.

5、Horizontal viewport was given unbounded height.

可用Flexible继续包裹解决 Horizontal viewport was given unbounded height.报错
eg:

  Flexible(
             child: TabBarView(
             controller: _controller,
             children: tabs.map((Tabs tab){
               return TravelTabPage(travelUrl:travelTabModel.url,
                                    params: travelTabModel.params ,
                                    groupChannelCode: tab.groupChannelCode,
                                    type:tab.type ,
                                    );
             }).toList(),
           ) ,
     )

6、 _TypeError (type 'double' is not a subtype of type 'int')

打全局断点的时候报错
说明定义的模型和服务器返回的类型不一致。将模型改变与服务器对应的类型即可

7、WARNING: CocoaPods requires your terminal to be using UTF-8 encoding.

图片.png
图片.png

或者以上步骤1,步骤2都使用终端操作
终端打开~/.bash_profile (open ~/.bash_profile)
4、编辑并添加 export LANG=en_US.UTF-8
5、退出并保存
6、重启VSCODE, 重新运行即可。

8、Failed assertion: line 319 pos 15: 'color == null || decoration == null'

图片.png

上面文字的意思是,container 这个容器组件中 color 和decoration不能同时存在。注释掉其中一个就可以了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 经历不少 多少带些老青春岁月的欢快 无知的不知所措 人能被治愈的是医院 心能被治愈的是自己 梦一场病一场,负了13...
    321小肥仔阅读 309评论 0 0
  • 橙色小蘑菇阅读 204评论 0 7
  • 我的写作之旅,最刚开始的写作应该就是小时候的小学生写作文了吧。记得最刚开始的写作是老师布置的作业,写日记,写的都是...
    思考中的小苏阅读 259评论 0 1
  • 像这样静静浪费时间的时刻,在我们的人生中数不胜数。等车,等人,侯考,候诊……有时候几分钟,有时候几个小时,有...
    82年的小葡萄阅读 516评论 0 1