问题记录
03-21
Flutter处理base图片
final base64 = url. split(",")[1];
final bytes = const Base64Decoder () . convert (base64);
image = ExtendedImage.memory(
bytes,
width: width,
height: height,
fit: fit,
shape: BoxShape. rectangle,
borderRadius: borderRadius,
mode: mode,
enableSlideOutPage: enableslideoutPage,
initGestureConfigHandler:initGestureConfig,
LoadStateChanged: (state) =>_buildLoadState (context, state),
);
03-14
flutter获取网络时间
利用百度(http://www.baidu.com)来获取网络时间
Future<DateTime> getNetworkTime() async {
try {
final response = await Dio().get('http://www.baidu.com');
// 获取响应头中的cookie
String cookie = response.headers.map['set-cookie'].toString();
// 使用split方法将Cookie字符串拆分成一个List
List<String> cookieList = cookie.split('; ');
// 遍历List,查找并提取PSTM的值
String pstmValue = '';
for (String cookie in cookieList) {
if (cookie.contains('PSTM')) {
RegExp regExp = RegExp(r'PSTM=([^;]+)');
Match match = regExp.firstMatch(cookie);
if (match != null && match.groupCount > 0) {
pstmValue = match.group(1);
break;
}
}
}
final timestamp = int.parse(pstmValue) * 1000; // 转换为毫秒
return DateTime.fromMillisecondsSinceEpoch(timestamp);
} catch (e) {
print('e:${e.toString()}');
return null;
}
}
解释一下,这段正则的意思。
在上述代码中,RegExp 实例的构造函数使用一个字符串参数,该参数包含一个正则表达式。正则表达式 r'PSTM=([^;]+)' 是一个匹配 PSTM 字符串和它的值的表达式,具体解释如下:
r 前缀表示这是一个原始字符串,其中的特殊字符不需要进行转义,因为正则表达式中经常使用反斜杠 \,所以使用原始字符串可以避免写很多冗长的转义符。
PSTM= 是一个字面量,表示匹配 PSTM 这个字符串。
() 是一个捕获组,表示匹配的子字符串会被捕获并存储在一个数组中,这个组只有一个,也就是我们需要提取的 PSTM 的值。
[^;]+ 是一个字符集,表示匹配除了分号 ; 之外的任何字符,加上 + 表示匹配至少一个字符。
因此,正则表达式 r'PSTM=([^;]+)' 的含义是匹配字符串中 PSTM 后跟等号 =,然后捕获除了分号 ; 之外的任何字符,这个任何字符的序列就是 PSTM 的值。
再解释一下,为什么要*1000
在 Dart 和很多其他编程语言中,时间戳的单位通常是毫秒(milliseconds),而不是秒(seconds)。而在 Unix 时间戳中,时间单位是秒。
因此,如果我们直接使用 Unix 时间戳来创建一个 DateTime 对象,那么它表示的是从 1970 年 1 月 1 日 00:00:00 UTC 到某个时间点的秒数,而不是毫秒数。为了将 Unix 时间戳转换为毫秒级别的时间戳,我们需要将其乘以 1000。这样就可以将 Unix 时间戳从秒级别转换为毫秒级别。
在上面的代码中,我们先将 Unix 时间戳 unixTimeStamp 乘以 1000,然后再将其转换为 DateTime 对象。这样可以将秒级别的 Unix 时间戳转换为毫秒级别的时间戳,从而正确地创建对应的 DateTime 对象。
03-13
标题这两个方法有什么区别,await file.exists()和await file.existsSync()
在Flutter中,这两个方法的区别在于:
await file.exists()
返回一个 Future 对象,表示该文件是否存在。如果文件存在,则返回 true,否则返回 false。调用该方法时,如果文件不存在,则需要等待文件被创建或者下载完毕。
await file.existsSync()
是一个同步方法,直接返回一个 bool 类型的值,表示该文件是否存在。如果文件存在,则返回 true,否则返回 false。这个方法不会阻塞当前线程的执行,因为它是同步的。
因此,如果你需要等待文件被创建或者下载完毕后再进行下一步操作,那么你应该使用 await file.exists()
方法。如果你不想阻塞当前线程的执行,并且不需要等待文件被创建或者下载完毕,那么你应该使用 file.existsSync()
方法。
01-13
Failed to launch emulator: Error: Emulator didn’t connected within 60 seconds
这是可能是由于电脑机器配置太低导致的连接超时问题, 使用下面的命令即可,看看原因。
flutter emulators #可以查看<emulator id>
flutter emulators --launch <emulator id>
本次遇到的问题是
The Android emulator exited with code 1 during startup
Android emulator stderr:
PANIC: Avd’s CPU Architecture ‘x86’ is not supported by the QEMU2 emulator on aarch64 host.
Address these issues and try again.
简单来说就是架构有问题,我使用的模拟器是x86的,换一个其他架构的模拟器就完事了。
调试 flutter 应用时崩溃:com.android.build.gradle.internal.crash.PluginCrashReporter
试试清除缓存mac
rm -rf $HOME/.gradle/caches/
如何清除gradle
降低版本即可
前:distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
后:distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
Flutter:Unhandled exception: FormatException: Could not find an option named “supper-arguments”.,
把supper换成super
官方的flutter webview 选择文件和拍照功能的,H5 页面有个拍照功能 点击没有反应
直接upgrade即可,因为官网的组件已经引用了。upgrade就是2.8.14+