【发布时间】:2022-10-23 18:00:16
【问题描述】:
我设法在我的应用程序中添加服务(创建 Excel 文件 - 创建 PDF 文件) 在这两种服务中,我都遇到了打开文件包的问题..
我使用的包:
- open_document 1.0.5
- open_filex: ^4.1.1
- 打开文件:^3.2.1
我为此目的使用了许多包,但我认为有一个问题我没有通过在 (AndroidManifest.xml) 文件 build.gradle (项目级别) 中添加脚本来解决它。
我会告诉你我的代码。
顺便说一句,我使用的是GetX包,所以有Controller类:
import 'dart:io';
import 'dart:typed_data';
import 'package:get/get.dart';
import 'package:open_document/open_document.dart';
import 'package:path_provider/path_provider.dart';
import 'package:uuid/uuid.dart';
import '../model/bill_model.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
class BillController extends GetxController {
static BillController instance = BillController();
List<String> options = ['Cleaning Tax', 'Running Costs', 'Delay Tax'];
List<BillModel> taxOptions = [
BillModel(id: const Uuid().v1(), name: 'Cleaning Tax'),
BillModel(id: const Uuid().v1(), name: 'Running Costs'),
BillModel(id: const Uuid().v1(), name: 'Delay Tax'),
];
List<BillModel> selectedOptions = [];
Future<Uint8List> createPDF() async {
final pdf = pw.Document();
pdf.addPage(
pw.Page(
build: (context) {
return pw.Center(
child: pw.Text('Hello World'),
);
},
),
);
update();
return pdf.save();
}
Future savePdfFile(String fileName, Uint8List byteList) async {
final output = await getTemporaryDirectory();
var filePath = '${output.path}/$fileName.pdf';
final file = File(filePath);
await file.writeAsBytes(byteList);
await OpenDocument.openDocument(filePath: filePath);
update();
}
}
这是我在 UI 类中调用它的地方:
GetBuilder<BillController>(
builder: (controller) => Padding(
padding: EdgeInsets.only(top: Dimensions.height80),
child: AddSaveButton(
title: 'Create bill',
fontSize: Dimensions.font24,
onPress: () async {
final data = await controller.createPDF();
controller.savePdfFile('invoice_5', data);
},
),
),
),
我遇到了这个问题:
Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
并通过 StackOverFlow Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null) 中的此链接获得解决方案
之后出现了新问题:app:processDebugResources 并在此链接中也得到了 StackOverFlow 中可接受的答案:Execution failed for task ':app:processDebugResources' in Flutter project
我的 AndroidManifest.xml 文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.water_collection">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.open_document_example.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
<application
android:label="Water collection"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
终于什么都没有改善,但还是不知道为什么?!!
任何帮助将不胜感激。
【问题讨论】:
标签: android excel flutter pdf createfile