Process Exception Operation not permitted when I try to execute command in my Flutter Application

138 Views Asked by At

I got an exception about Operation not permitted when I tried to execute the command.

I use youtubedr, This app works for downloading youtube videos. I already test it in the terminal and it works smoothly.

And I already tried await Process.start('ls',['-la', 'Downloads']); in the Flutter app and it does not have an Operation not permitted error.

I use MacOS 13.4.1 (c) and This is my Flutter Version

Flutter 3.10.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision f468f3366c (6 weeks ago) • 2023-07-12 15:19:05 -0700
Engine • revision cdbeda788a
Tools • Dart 3.0.6 • DevTools 2.23.1

This is my Flutter code

import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';

class HomeView extends StatefulWidget {
  const HomeView({super.key});

  @override
  State<HomeView> createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  String outputText = ''; // To store output information
  double downloadProgress = 0.0;

  Future<void> downloadVideo() async {
    String command = "/opt/homebrew/bin/youtubedr";
    List<String> arguments = [
      'download',
      // '-d Downloads',
      '-o xuxong.mp3',
      '-q 140' ,
      'https://www.youtube.com/watch\?v\=j6wNBRaoBtc\&t\=1444s',
    ];
    Process process = await Process.start(command, arguments);

    // Capture standard output and error streams
    process.stdout.transform(utf8.decoder).listen((data) {
      setState(() {
        outputText += data; // Append output to the existing text
      });
    });

    // Wait for the process to complete
    int exitCode = await process.exitCode;
    if (exitCode == 0) {
      print('Download successful');
    } else {
      print('Download failed');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text("Youtube Downloader"),
      ),
      body: Container(
        margin: const EdgeInsets.all(16.0),
        child: Center(
          child: Column(
            children: [
              LinearProgressIndicator(value: downloadProgress),
              SizedBox(
                width: double.infinity,
                height: 400,
                child: SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  physics: AlwaysScrollableScrollPhysics(),
                  child: Column(
                    children: <Widget>[
                      Text(outputText),
                    ],
                  ),
                  controller: ScrollController(keepScrollOffset: false),
                ),
              ),
              ElevatedButton(
              onPressed: downloadVideo,
                child: const Text('Download'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

0

There are 0 best solutions below