tftp 1.0.1

tftp plugin #

A lib for Trivial File Transfer Protocol(TFtp) server and client.

How To Use #

Server #

We can start a tftp server, and then a tftp client could download/upload file from server.

The server has onRead,onWrite and onError event.

For example:

  var server = await TFtpServer.bind("0.0.0.0", port: 6699);
  server.listen((socket) {
    socket.listen(onRead: (file, onProcess) {
      print("read file from server:$file");
      onProcess(progressCallback: (count, total) {
        print("read:${count / total}%");
      });
      return "$readAblePath/$file"; // server will read file from return value
    }, onWrite: (file, doTransform) {
      doTransform();
      infoController.add("write file to server:$file");
      return "$writeAblePath/$file"; // server will write file to return value
    }, onError: (code, msg) {
      infoController.add("Error[$code]:$msg");
    });
  });

client #

We can create a tftp client to download/upload file to a tftp server.

  1. init

var client = await TFtpClient.bind("0.0.0.0", port);

  1. Get

client.get(localFilePath, remoteFilePath,remoteHost, remotePort);

  1. Put

client.put(localFilePath, remoteFilePath,remoteHost, remotePort);

Topic: #

If you want to read/write file to EXTERNAL STORAGE,you should Add permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

[1.0.0] - Release tftp

  • Release a simple tftp server and tftp client

[1.0.1] - fix bug.

  • fix bug.
  • format code.
  • fix document.

example/README.md

example #

Flutter TFtp server #

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tftp/tftp.dart';

class ServerDemo extends StatefulWidget {
  @override
  _ServerDemoState createState() => _ServerDemoState();
}

class _ServerDemoState extends State<ServerDemo> {
  StreamController<double> processController;
  StreamController<String> infoController;
  TFtpServer server;

  @override
  void initState() {
    super.initState();
    processController = StreamController();
    infoController = StreamController();
  }

  @override
  void dispose() {
    processController.close();
    infoController.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[
          StreamBuilder<String>(
            stream: infoController.stream,
            builder: (context, snapshot) {
              return snapshot.hasData ? Text(snapshot.data) : Container();
            },
          ),
          RaisedButton(
            onPressed: () async {
              Directory appDocDir = await getApplicationDocumentsDirectory();
              String appDocPath = appDocDir.path;

              server = await TFtpServer.bind("0.0.0.0", port: 6699);
              server.listen((socket) {
                socket.listen(onRead: (file, onProcess) {
                  infoController.add("read file from server:$file");
                  onProcess(progressCallback: (count, total) {
                    processController.add(count / total);
                  });
                  return "$appDocPath/$file";
                }, onWrite: (file, doTransform) {
                  doTransform();
                  infoController.add("write file to server:$file");
                  return "$appDocPath/$file";
                }, onError: (code, msg) {
                  infoController.add("Error[$code]:$msg");
                });
              });
              infoController.add("Server start.");
            },
            child: Text("Start"),
          ),
          RaisedButton(
            onPressed: () {
              if (null != this.server) {
                this.server.close();
                infoController.add("Server stop.");
              }
            },
            child: Text("Stop"),
          ),
          StreamBuilder<double>(
            stream: processController.stream,
            builder: (context, snapshot) {
              return snapshot.hasData
                  ? Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text("Process:${(snapshot.data * 100).toStringAsFixed(2)}%"),
                        LinearProgressIndicator(
                          value: snapshot.data,
                        ),
                      ],
                    )
                  : Container();
            },
          )
        ],
      ),
    );
  }
}

Flutter TFtp client #

import 'dart:io';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tftp/tftp.dart';

class ClientDemo extends StatefulWidget {
  @override
  _ClientDemoState createState() => _ClientDemoState();
}

class _ClientDemoState extends State<ClientDemo> {
  var host = TextEditingController();
  var port = TextEditingController();
  var local = TextEditingController();
  var remote = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Container(
        child: Column(
      children: ListTile.divideTiles(
        context: context,
        tiles: [
          TextField(
            controller: host,
            decoration: InputDecoration(labelText: "Host"),
          ),
          TextField(
            controller: port,
            decoration: InputDecoration(labelText: "Port"),
          ),
          TextField(
            controller: local,
            decoration: InputDecoration(labelText: "Local File"),
          ),
          TextField(
            controller: remote,
            decoration: InputDecoration(labelText: "Remote File"),
          ),
          Row(
            children: <Widget>[
              RaisedButton(
                child: Text("Get"),
                onPressed: () async {
                  var client = await TFtpClient.bind(
                      "0.0.0.0", 40000 + Random().nextInt(10000));
                  Directory appDocDir =
                      await getApplicationDocumentsDirectory();
                  String appDocPath = appDocDir.path;
                  client.get("$appDocPath/${local.text}", remote.text,
                      host.text, int.parse(port.text));
                },
              ),
              RaisedButton(
                child: Text("Put"),
                onPressed: () async {
                  var client = await TFtpClient.bind(
                      "0.0.0.0", 40000 + Random().nextInt(10000));
                  Directory appDocDir =
                      await getApplicationDocumentsDirectory();
                  String appDocPath = appDocDir.path;
                  client.put("$appDocPath/${local.text}", remote.text,
                      host.text, int.parse(port.text));
                },
              ),
            ],
          ),
        ],
      ).toList(),
    ));
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  tftp: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:tftp/tftp.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
16
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
57
Learn more about scoring.

We analyzed this package on Sep 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-1 points)

89 out of 89 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test