Flutter 如果项目中有dart html导入,如何在模拟器上运行flatter
我正在为手机和网络应用程序使用flutter。我需要从移动应用程序和web浏览器上传照片。移动应用程序没有问题,一切正常,而对于web版本,发明了以下解决方案:我为web应用程序创建了一种方法,以便可以从桌面上传照片。为此,我使用了Flutter 如果项目中有dart html导入,如何在模拟器上运行flatter,flutter,dart,flutter-web,Flutter,Dart,Flutter Web,我正在为手机和网络应用程序使用flutter。我需要从移动应用程序和web浏览器上传照片。移动应用程序没有问题,一切正常,而对于web版本,发明了以下解决方案:我为web应用程序创建了一种方法,以便可以从桌面上传照片。为此,我使用了import'dart:html',但如果我尝试在web浏览器中运行一切正常,但如果我现在在移动应用程序上运行它,我会收到一系列错误,说明无法编译web导入(html) 我读了下面的一期,它说你不能在移动应用程序中调用HTML方法。如果应用程序未在web浏览器中运行,
import'dart:html'代码>,但如果我尝试在web浏览器中运行一切正常,但如果我现在在移动应用程序上运行它,我会收到一系列错误,说明无法编译web导入(html)
我读了下面的一期,它说你不能在移动应用程序中调用HTML方法。如果应用程序未在web浏览器中运行,我禁止调用HTML方法,但结果相同:
导入'dart:html'代码>
Future<void> _listenImagesFromWeb() async {
if (!kIsWeb)
return;
final String profileId = (await FirebaseAuth.instance.currentUser()).uid;
final completer = Completer<List<String>>();
InputElement uploadInput = FileUploadInputElement();
uploadInput.multiple = true;
uploadInput.accept = 'image/*';
uploadInput.click();
uploadInput.addEventListener('change', (e) async {
final files = uploadInput.files;
Iterable<Future<String>> resultsFutures = files.map((file) {
final reader = FileReader();
reader.readAsDataUrl(file);
reader.onError.listen((error) => completer.completeError(error));
return reader.onLoad.first.then((_) => reader.result as String);
});
final results = await Future.wait(resultsFutures);
completer.complete(results);
});
//* need to append on mobile safari
document.body.append(uploadInput);
final List<String> images = await completer.future;
this.setState(() {
_isLoading = true;
});
await addFilesToStorage(widget.agreement.id, images, profileId);
uploadInput.remove();
}
错误:找不到:“dart:html”我会使用这个漂亮的项目[https://pub.dev/packages/universal_html]如果您按照他们的指示进行操作,这正是您所需要的:
只需将dart:html导入替换为包:universal_html/html.dart。
在浏览器中,将自动使用dart:html
它将确保代码在网络和移动目标上都能编译。对于web,它将自动导入dart:html;对于移动设备,它将提供虚拟功能
await _listenImagesFromWeb().then((value) =>
setState(() {
images = loadImage();
})
);
updateState();
}
floatingActionButton: kIsWeb
? SpeedDial(
animatedIconTheme: IconThemeData(size: 22.0),
child: Icon(Icons.add),
closeManually: false,
children: [
SpeedDialChild(
child: Icon(Icons.photo_library),
label: translate('agreement.image_uploader.select_images_button', context: context),
onTap: () => _callAddImagesFromWeb(context)),
],
)
: SpeedDial(
animatedIconTheme: IconThemeData(size: 22.0),
child: Icon(Icons.add),
closeManually: false,
children: [
SpeedDialChild(
child: Icon(Icons.camera_alt),
label: translate('agreement.image_uploader.take_picture_button', context: context),
onTap: () => _addMultiplyFile(context, true)),
SpeedDialChild(
child: Icon(Icons.photo_library),
label: translate('agreement.image_uploader.select_images_button', context: context),
onTap: () => _addMultiplyFile(context, false)),
],
),