ftp_server 0.0.1 copy "ftp_server: ^0.0.1" to clipboard
ftp_server: ^0.0.1 copied to clipboard

This package provides a simple FTP server implementation in Dart. It supports both read-only and read-and-write modes, making it suitable for various use cases. The server allows clients to connect an [...]

example/lib/main.dart

import 'dart:io';
import 'dart:isolate';
import 'package:flutter/material.dart';
import 'package:ftp_server/ftp_server.dart';
import 'package:ftp_server/server_type.dart';
import 'package:path_provider/path_provider.dart';

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

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

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  FtpServer? _ftpServer;
  String _serverStatus = 'Server is not running';
  String _connectionInfo = 'No connection info';
  bool _isLoading = false;
  Isolate? _isolate;
  ReceivePort? _receivePort;

  Future<void> _toggleServer() async {
    setState(() {
      _isLoading = true;
    });

    if (_ftpServer == null) {
      var server = FtpServer(21,
          startingDirectory: (await getDownloadsDirectory())!.path,
          allowedDirectories: [(await getDownloadsDirectory())!.path],
          serverType: ServerType.readOnly);
      Future serverFuture = server.start();

      _ftpServer = server;
      var address = InternetAddress
          .anyIPv4; // Modify this to retrieve the correct network interface
      setState(() {
        _serverStatus = 'Server is running';
        _connectionInfo =
            'Connect using IP: ${address.address}, Port: ${_ftpServer!.port}';
        _isLoading = false;
      });
      await serverFuture;
    } else {
      await _ftpServer!.stop();
      _ftpServer = null;
      setState(() {
        _serverStatus = 'Server is not running';
        _connectionInfo = 'No connection info';
        _isLoading = false;
      });
    }
  }

  @override
  void dispose() {
    _receivePort?.close();
    _isolate?.kill(priority: Isolate.immediate);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Flutter FTP Server'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(_serverStatus),
              const SizedBox(height: 20),
              Text(_connectionInfo),
              const SizedBox(height: 20),
              _isLoading
                  ? const CircularProgressIndicator()
                  : ElevatedButton(
                      onPressed: _toggleServer,
                      child: Text(
                          _ftpServer == null ? 'Start Server' : 'Stop Server'),
                    ),
            ],
          ),
        ),
      ),
    );
  }
}
4
likes
0
pub points
67%
popularity

Publisher

verified publisherzcreations.info

This package provides a simple FTP server implementation in Dart. It supports both read-only and read-and-write modes, making it suitable for various use cases. The server allows clients to connect and perform standard FTP operations such as listing directories, retrieving files, storing files, and more.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, intl

More

Packages that depend on ftp_server