1

If I click on file how to open that file inside flutter app. This is what I have done:

This code for show flies in listView:

ListView.builder(
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemCount: attachments.length,
              itemBuilder: (BuildContext ctxt, int index) {
                print (index);
                return ListView(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    children: <Widget>[
                      Padding(
                        padding: const EdgeInsets.all(1.0),
                        child: ListTile(
                          leading: CircleAvatar(
                            child: _getIcon(
                                attachments[index].split('/').removeLast()),
                            backgroundColor: Colors.white,
                          ),
                          subtitle:
                          Text(attachments[index].split('/').removeLast()),
                          onTap: (){}
                        ),
                      ),
                    ]);
              })

I have used open_file package but its not working this is code i have done:

            onTap: () async {
            String fileUrl = widget.schoolDcUrl + "/container"attachments[index];
            final _openFile = await OpenFile.open(fileUrl);
             Logger.i(_openFile);
                              }

When I click on file it does not opening the file it will give a message on terminal file not found.

I want onTap to open that file in application.

alireza easazade
  • 3,324
  • 4
  • 27
  • 35
shilpa navale
  • 409
  • 3
  • 9
  • 18
  • Open files in the app will require a lot of code to be able to handle all formats, it is strongly recommended to use a dedicated application, you can use: https://pub.dev/packages/open_file – huextrat Feb 12 '20 at 14:09
  • I have used open_file package but its not working--This is code i have done onTap: () async { String fileUrl = widget.schoolDcUrl + "/container" + attachments[index]; String fileUrl1 = Uri.encodeFull(fileUrl); final _openFile = await OpenFile.open(fileUrl); Logger.i(_openFile); } – shilpa navale Feb 13 '20 at 04:11
  • this is perfect answer https://stackoverflow.com/a/60203399/15601357 without any boilerplate – Kururu95 Jul 03 '21 at 20:32

3 Answers3

2

In pubspec.yaml add:

dev_dependencies:
   open_file: ^3.0.1

In code add:

import 'package:open_file/open_file.dart';

OpenFile.open("/sdcard/example.pptx");


open_file documentation:
https://pub.dev/packages/open_file#-readme-tab-

Jayadev Haddadi
  • 342
  • 3
  • 12
0

You should be able to use the flutter_filereader package that opens all of those file formats.

Make sure to follow the configurations and the example on the README.

J. S.
  • 8,905
  • 2
  • 34
  • 44
0

without downloading file, you can open from URL using the package : url_launcher 5.4.1

_launchURL(String url) async {
  const url = url; // you can use your url
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Unable to open url : $url';
  }
}