file_upload 0.0.3 copy "file_upload: ^0.0.3" to clipboard
file_upload: ^0.0.3 copied to clipboard

a package to send images/videos in a request to the server

example/main.dart

import 'package:flutter/material.dart';
import 'package:file_upload/file_upload.dart';
import 'package:image_picker/image_picker.dart';
import 'package:file_picker/file_picker.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'file upload example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: FileUploadExample(
        title: 'file upload example',
      ),
      // home: AudioScreen(),
    );
  }
}

class FileUploadExample extends StatefulWidget {
  FileUploadExample({Key? key, required this.title}) : super(key: key);

  final String title;

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

class _FileUploadExampleState extends State<FileUploadExample> {
  FileUpload fileUpload = FileUpload();
  Map<String, dynamic> response1 = {};
  List response2 = [];
  final ImagePicker _picker = ImagePicker();
  PickedFile? file1;
  PickedFile? file2;
  FileType? _pickingType = FileType.custom;
  String? path1;
  String? path2;
  String? path3;
  //In this example we use the localhost url but you will change the url
  // for the url sever
  String _url = 'http://10.0.2.2:5000';

  Future getVideoFromGallery() async {
    file1 = await _picker.getVideo(source: ImageSource.gallery);
    setState(() {
      path1 = file1?.path;
      print(path1);
    });
  }

  Future getImageFromGallery() async {
    file2 = await _picker.getImage(source: ImageSource.gallery);
    setState(() {
      path2 = file2?.path;
      print(path2);
    });
  }

  void _pickFile3() async {
    FilePicker.platform.pickFiles(
      type: _pickingType!,
      onFileLoading: (FilePickerStatus status) => print(status),
      allowedExtensions: ['mp3', 'wav'],
    ).then((paths) async {
      path3 = paths?.files![0].path!.substring(1);
      print(path3);
    });
  }

  Future<void> sendFiles() async {
    String url = '$_url/request1';
    /*
    uploadTwoFiles()
    Params:
      String url,
      String fileKey1,
      String filePath1,
      String fileType1,
      String fileKey2,
      String filePath2,
      String fileType2
    */
    print(url);
    response1 = await fileUpload.uploadTwoFiles(
        url, 'video', path1!, 'mp4', 'image', path2!, 'jpg');
    print(response1);
  }

  Future<void> sendFile() async {
    String url = '$_url/request2';
    /*uploadFile()
  Params:
      String url,
      String fileKey,
      String filePath,
      String fileType)
  */
    response2 = await fileUpload.uploadFile(url, 'audio', path3!, 'wav');
    print(response2);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
                onPressed: () {
                  getVideoFromGallery();
                },
                child: Text('pick file1')),
            ElevatedButton(
                onPressed: () {
                  getImageFromGallery();
                },
                child: Text('pick file2')),
            ElevatedButton(
                onPressed: () {
                  sendFiles();
                },
                child: Text('send files')),
            Text('$response1'),
            ElevatedButton(
                onPressed: () {
                  _pickFile3();
                },
                child: Text('pick file3')),
            ElevatedButton(
                onPressed: () {
                  sendFile();
                },
                child: Text('send file')),
            Text('$response2'),
          ],
        ),
      ),
    );
  }
}
2
likes
100
pub points
70%
popularity

Publisher

verified publishercodingminds.academy

a package to send images/videos in a request to the server

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

Dependencies

flutter, http, http_parser

More

Packages that depend on file_upload