I managed to add to services in my app (Create Excel file - Create PDF file) In both services I had a problem with open file packages..
Packages I used:
- open_document 1.0.5
- open_filex: ^4.1.1
- open_file: ^3.2.1
I used many packages for that purpose, but I think there is a problem I didn't catch it by adding scripts in (AndroidManifest.xml) file, build.gradle (project level).
I will show you my code.
By the way, I'm using GetX package, so there is the Controller class:
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();
}
}
And here is where I call it in the UI class:
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);
},
),
),
),
I faced this problem:
Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
and got a solution form this link in StackOverFlow Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
After that got new problem : app:processDebugResources
and got the acceptable answer in StackOverFlow too in this link: Execution failed for task ':app:processDebugResources' in Flutter project
my AndroidManifest.xml file:
<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>
Finally nothing had improved, but still didn't know why?!!
Any help will be appreciated.
CodePudding user response:
Finally after a week of searching, I have got a solution.. All what I did was correct, but the problem was in the "Open file" packages.. All packages I used have problems.
At the end I use a package called open_file_plus
and every thing worked fine.
I got some information from this URL: Open file by default application flutter