卡在flutter pub get不动

1
2
3
4
5
6
7
8
9
10
11
//命令行输入
vim ~/.bash_profile

//添加下面三行,保存
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn
export PATH=/Users/计算机名称/flutter/bin:$PATH //下载的flutterSDK的路径

//命令行输入
source ~/.bash_profile
flutter -h

运行 flutter doctor 命令,再 package get

运行按钮灰色:not applicable for the “main.dart” configuration

是因为 Flutter SDK 没有设置:File > setting> Language & Framework > flutter

java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7

目录/android/gradle/wrapper/gradle-wrapper.properties 修改 distributionUrl属性。

例如:distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip 改为 distributionUrl=file:///D:/DevKits/gradle/gradle-6.4.1-all.zip(这是我本地下载好了的)

setState() called after dispose()

State 对象被从对象数卸载释放之后再次调用 setState 就会报 setState() called after dispose()。

State 的 mounted 源码:

1
2
3
4
5
6
7
8
9
10
/// Whether this [State] object is currently in a tree.
///
/// After creating a [State] object and before calling [initState], the
/// framework "mounts" the [State] object by associating it with a
/// [BuildContext]. The [State] object remains mounted until the framework
/// calls [dispose], after which time the framework will never ask the [State]
/// object to [build] again.
///
/// It is an error to call [setState] unless [mounted] is true.
bool get mounted => _element != null;

注释中说得很清楚:判断 State 对象现在还在不在对象树中。

So,解决方案就是在 setState 之前先判断一下该 State 是否已经被释放:

1
2
3
4
5
6
/// 更新用户状态
void updateState(fn){
if (mounted) {
setState(fn);
}
}

MissingPluginException(No implementation found for method initialize on channel dexterous.com/flutter/local_notifications)

报丢失插件错误,是因为“hot reload”问题。

我们新添加的插件包就无法直接通过热重载打入手机应用包中,有的人会尝试直接点击绿色向右小三角安装,这样也是无法实现重新编译打包安装到手机的,这个时候你必须停止掉应用(红色小正方形按钮),再重新点击安装才行。