"Open with" question for flutter desktop apps

439 views Asked by At

If I have a flutter app on Windows and use "open with" to open some files with the app, how can I know what files were requested to open? For example, I select one or more files and "open with" the flutter app which would then display the files in a list. This would apply with associated files too, I assume.

Where can I get that list of files?

1

There are 1 answers

0
mirkaim On

Richard's answer works. Here is a quick little implementation if anybody needs to test it out. I am just needing to know what files the app were opened with "Open with..." or when files are dragged to the exe and this works for me as of Dec 2022 on Flutter 3.3.9.

import 'package:flutter/material.dart';

late List<String> globalArgs = [];

void main(List<String> args) {
  final List<String> fakeArgs = [
    'string 01',
    'string 02',
    'string 03',
  ];

  // * uncomment this line to verify that the list works
  //args = fakeArgs;

  runApp(MyApp(args: args));
}

class MyApp extends StatefulWidget {
  const MyApp({
    super.key,
    required this.args,
  });

  final List<String> args;

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    // * set global args to command line args here for simplification
    // * instead passing them command line arguments over the place.
    globalArgs = widget.args;

    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const MyHomePage(
          title: 'Command line arguments test',
        ));
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<int> colorCodes = <int>[600, 500, 100];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
        padding: const EdgeInsets.all(8),
        itemCount: globalArgs.length,
        itemBuilder: (BuildContext context, int index) {
          return Container(
            height: 50,
            color: Colors.amber[colorCodes[index]],
            child: Center(child: Text('[${globalArgs[index]}]')),
          );
        },
      ),
    );
  }
}