dart 代码中判断当前平台是否是 ohos
import 'package:flutter/foundation.dart';
bool isOhos() {
return defaultTargetPlatform == TargetPlatform.ohos;
}
复制代码
代码中存在 Platform.isOhos 会导致 fluttn run、flutter build har、flutter attach 失败
问题现象:如果 flutter 代码中存在 Platform.isOhos,如下:
if (Platform.isAndroid || Platform.isOhos) {
print("test");
}
复制代码
会导致 flutter run、flutter build har、 flutter attach(不指定本地引擎产物,依赖服务器的引擎产物)失败,报错信息:
解决方法:请将 Platform.isOhos 修改成 defaultTargetPlatform == TargetPlatform.ohos
flutter 鸿蒙原生端获取到图片资源
问:在使用 plugin 时, 鸿蒙会返回这个类型的对象 binding: FlutterPluginBinding,使用这个对象的 binding.getFlutterAssets().getAssetFilePathByName('xxxx') 获取 flutter 代码库中的图片资源时,鸿蒙原生端无法获取到图片资源(鸿蒙端直接用 Image(this.img)方法加载)。有什么别的方法能够获取到?
答:binding.getFlutterAssets().getAssetFilePathByName('xxxx')得到的是资源路径,加载原生图片资源可以参考以下实现
import { image } from '@kit.ImageKit';
@Component
export struct DemoComponent {
@Prop params: Params
viewManager: DemoView = this.params.platformView as DemoView
image?: string
@State imageSource:image.ImageSource|null=null
async aboutToAppear() {
let args: HashMap<string, object | string> = this.viewManager.args as HashMap<string, object>
this.image = args.get('src') as string
let rmg = DemoPluginAssetPlugin.binding.getApplicationContext(). resourceManager;
let rawfile = await rmg.getRawFileContent("flutter_assets/${this.image}");
let buffer = rawfile.buffer.slice(0);
this.imageSource = image.createImageSource(buffer);
}
build() {
Column(){
if(this.imageSource){
Image(this.imageSource.createPixelMapSync())
}
}
}
// aboutToAppear(): void {
// let args: HashMap<string, object | string> = this.viewManager.args as HashMap<string, object>
// this.image = args.get('src') as string
// }
// build() {
// //todo 问题点
// // Image(this.image)
// Image(DemoPluginAssetPlugin.binding.getFlutterAssets().getAssetFilePathByName (this.image))
// // Image(DemoPluginAssetPlugin.binding.getFlutterAssets(). getAssetFilePathBySubpath(this.image))
// }
}
复制代码
问:let rawfile = await rmg.getRawFileContent("flutter_assets/"+this.image ); 这行代码会触发 build 方法么? 为什么我打断点打到这一行,然后继续执行断点直接就到 build 方法了?
答:let rawfile = await rmg.getRawFileContent("flutter_assets/"+this.image );这行代码为耗时操作,debug 时会暂不执行当前方法的剩余代码直到耗时操作返回结果,而进入 build 只是正常渲染流程
评论