Skip to content

Instantly share code, notes, and snippets.

@rohankandwal
Created April 29, 2023 20:59
Show Gist options
  • Save rohankandwal/c7dd80627fdade779d1e6154657cecf1 to your computer and use it in GitHub Desktop.
Save rohankandwal/c7dd80627fdade779d1e6154657cecf1 to your computer and use it in GitHub Desktop.
File Upload using WebView on Android
/// Read details on https://theprogrammingway.com/flutter-file-upload-using-webview-on-android/
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:webview_flutter_android/webview_flutter_android.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late final WebViewController controller;
@override
void initState() {
controller = WebViewController();
controller.loadRequest(
Uri.parse("https://theprogrammingway.com/html-file-upload/"));
addFileSelectionListener();
super.initState();
}
void addFileSelectionListener() async {
if (Platform.isAndroid) {
final androidController = controller.platform as AndroidWebViewController;
await androidController.setOnShowFileSelector(_androidFilePicker);
}
}
@override
Widget build(BuildContext context) {
return WebViewWidget(controller: controller);
}
/// Function for file selection from gallery
Future<List<String>> _androidFilePicker(FileSelectorParams params) async {
final result = await FilePicker.platform.pickFiles();
if (result != null && result.files.single.path != null) {
final file = File(result.files.single.path!);
return [file.uri.toString()];
}
return [];
}
}
@acx70
Copy link

acx70 commented May 16, 2024

Is there a way to do the same thing for ios?

@Emanoel7S
Copy link

======== Exception caught by services library ======================================================
The following MissingPluginException was thrown during a platform message callback:
MissingPluginException(No implementation found for method any on channel miguelruivo.flutter.plugins.filepicker)

When the exception was thrown, this was the stack:
#0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:332:7)

#1 MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:520:35)

#2 FilePickerIO._getPath (package:file_picker/src/file_picker_io.dart:103:33)

#3 _MyHomePageState._androidFilePicker (package:teste_custom_tabs/main.dart:62:20)

#4 WebChromeClientFlutterApi.setup. (package:webview_flutter_android/src/android_webview.g.dart:2455:40)

#5 BasicMessageChannel.setMessageHandler. (package:flutter/src/services/platform_channel.dart:235:36)

#6 _DefaultBinaryMessenger.setMessageHandler. (package:flutter/src/services/binding.dart:618:22)

@NEXTink
Copy link

NEXTink commented Oct 21, 2024

I don't know how to thank you enough for this, just gave you a follow thanks.

@rohankandwal
Copy link
Author

There is nothing special to be done for iOS. The only problem is special configuration required for android.

@rohankandwal
Copy link
Author

@Emanoel7S This looks like issue on filepicker, check if you have configured it properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment