Hey guys, I need assistance as I'm facing issues regarding a functionality I'm learning.
Scenario:
I want the user to be able to download a wallpaper by clicking the download button. The imageUrl is being fetched using an API Call however, I'm unable to parse even with a custom action.
Custom Action:
import 'dart:async';
import 'dart:io' show File, Path;
import 'package:http/http.dart' as http;
import 'dart:core';
import '/backend/backend.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
Future<void> downloadFile(String fileUrl) async {
final response = await http.get(Uri.parse(fileUrl));
if (response.statusCode == 200) {
final fileName = response.request!.url.toString().split('/').last;
final file = File('/storage/emulated/0/Download/${fileName}');
final fileSink = file.openWrite();
fileSink.write(response.bodyBytes);
await fileSink.flush();
await fileSink.close();
} else {
throw Exception('Failed to download file');
}
}
// Set your action name, define your arguments and return parameter,
// and then add the boilerplate code using the button on the right!
Outcome:
Nothing happens on the button click.
Kindly suggest some better approaches to this. Thank you!