skip to Main Content

Im noob in flutter, i try to recreate this proses,create, save then launch pdf, this app has 2 dart file:

1. main.dart

import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdf/pdf.dart';
import 'mobile.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {...}

class MyHomePage extends StatefulWidget {...}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          child: Text('Cliick Me'),
          onPressed: _createPDF,
        ),
      ),
    );
  }

  Future<void> _createPDF() async {
    PdfDocument document = PdfDocument();
    final page = document.pages.add();
    page.graphics.drawString(
      'welcome',
      PdfStandardFont(PdfFontFamily.helvetica, 30)
    );
    List<int> bytes = document.save();
    document.dispose();
    //FileSaveHelper.saveAndLaunchFile(bytes, 'Outfile.pdf');
    saveAndLaunchFile(bytes, 'Outfile.pdf');
  }

2.mobile.dart

import 'dart:io' as io;
import 'dart:io';
import 'package:open_file/open_file.dart';
import 'package:path_provider/path_provider.dart';

Future<void> saveAndLaunchFile(List<int> bytes, String fileName) async {
  try {
    final path = (await getExternalStorageDirectory())!.path;

    print('filepath : $path/$fileName');
    String data='empty';
    print('data pos1= $data');
    data = (await io.File('$path/$fileName').exists()).toString();
    final file = File('$path/$fileName');
    await file.writeAsBytes(
        bytes,
        flush: true
    );
    print('data pos2= $data');
    OpenFile.open('$path/$fileName');
    print('done');
  }
  catch (e) {
    print('error : $e');
  }
}

Emulator View

Now when I press ‘click me’, it does nothing, it supposed to show ‘welcome’ String from main.dart

the output from mobile.dart are bellow:

Syncing files to device Android SDK built for x86...
Reloaded 1 of 955 libraries in 310ms.
I/flutter ( 3688): filepath : `/storage/emulated/0/Android/data/com.cg2.my_first_try/files/Outfile.pdf`
I/flutter ( 3688): data pos1= empty
I/flutter ( 3688): data pos2= true
I/flutter ( 3688): done

The funny things, the day before, when i first install android studio, flutter and run this program, it was working. Then I updated dependency on yaml file, then on this perticular line on mobile.dart, asking

final path = (await getExternalStorageDirectory()).path; generate error ....potentialy null.

so i change into :

final path = (await getExternalStorageDirectory())!.path;

lastly, iam using ubuntu 20.04, i just need to understand whats going on, is it androdi studio or emulator problem, or do linux need permision to getExternalStorageDirectory. Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    Its seem ths java version that causing this. after googling, it turns out openJdk16 has campatible issues, messing up gradle creation. So i downgrade to 11, so far looks good.


  2. To use jdk 16 you should upgrade to:

    android/gradle/wrapper/gradle-wrapper.properties

    distributionUrl=https://services.gradle.org/distributions/gradle-7.0.2-all.zip
    

    android/build.gradle

    classpath 'com.android.tools.build:gradle:7.0.1'
    

    my jdk

    java 16.0.1 2021-04-20
    Java(TM) SE Runtime Environment (build 16.0.1+9-24)
    Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search