Search code examples
csvflutterlaunch

how to open a 'csv file' like 'url launcher' in Flutter


i converted list into File of .csv extension then tried OpenFile.open and ended up with error No permissions found in manifest for: 2, tried canLaunch and ended up with error name.csv exposed beyond app through Intent.getData(), Failed to handle method call

so how to open that csv file in any 3rd part application.


Solution

  • You can copy paste run full code below
    and make sure you have a file /sdcard/Download/sample.csv, see picture below
    You also need CSV Viewer installed in your Emulator
    code snippet

        final filePath = '/sdcard/Download/sample.csv';
        print('${filePath}');
        final message = await OpenFile.open(filePath);
    

    working demo

    enter image description here

    device file explorer

    enter image description here

    full code

    import 'package:flutter/material.dart';
    import 'dart:async';
    import 'package:open_file/open_file.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      String _openResult = 'Unknown';
    
      Future<void> openFile() async {
        //final filePath = '/sdcard/Download/sample.pdf';
        final filePath = '/sdcard/Download/sample.csv';
        print('${filePath}');
        final message = await OpenFile.open(filePath);
    
        setState(() {
          _openResult = message;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Scaffold(
            appBar: new AppBar(
              title: const Text('Plugin example app'),
            ),
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text('open result: $_openResult\n'),
                  FlatButton(
                    child: Text('Tap to open file'),
                    onPressed: openFile,
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }