Tuesday, May 30, 2023
HomeiOS Developmentandroid - I am growing an software in Flutter that connects to...

android – I am growing an software in Flutter that connects to NASA’s APOD API and I am having an error asking the consumer for permission


The applying has a button that the consumer clicks and calls a DatePicker in order that he selects the date he needs, primarily based on the date the appliance returns the picture of the day from the APOD API together with the title, description and a pair of buttons, the Obtain and Share, I am having issues with this Obtain button, as a result of when the consumer clicks on it the _requestPermission() technique is known as, however this technique is just not working, it doesn’t seem to approve or deny the permission it mechanically denies the permission with out even asking to the consumer, I need assistance with these strategies, right here is the code of the strategies which are referred to as when the consumer clicks the obtain button:

Future<String?> _getDownloadsDirectory() async {
    ultimate listing = await getExternalStorageDirectory();
    if (listing != null) {
      ultimate downloadsDirectory = '${listing.path}/Downloads';
      return downloadsDirectory;
    }
    return null;
  }

  Future<void> _requestPermission(String imageUrl) async {
    var standing = await Permission.storage.standing;
    if (!standing.isGranted) {
      standing = await Permission.storage.request();
      if (!standing.isGranted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content material:
                Textual content('É necessário permitir o acesso para salvar a imagem.'),
          ),
        );
        return;
      }
    } else if (standing.isPermanentlyDenied) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content material: Textual content(
              'A permissão de acesso ao armazenamento foi permanentemente negada.'),
        ),
      );
      return;
    }

    ultimate outcome = await _getDownloadsDirectory();
    if (outcome != null) {
      _downloadImage(imageUrl, outcome);
    }
  }

  void _downloadImage(String imageUrl, String directoryPath) async {
    ultimate response = await http.get(Uri.parse(imageUrl));
    ultimate imageBytes = response.bodyBytes;

    const fileName="imagem-do-dia.jpg";
    ultimate filePath="$directoryPath/$fileName";
    ultimate file = File(filePath);
    await file.writeAsBytes(imageBytes);

    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content material: Textual content('Imagem salva em $directoryPath'),
      ),
    );
  }

Please can anybody assist me as that is the primary app I’m growing.
It’s value remembering that I’ve already added the next permissions to the AndroidManifest.xml file and the model of Android I’m working the appliance on is Android 13 and I’ve already made the required imports:

import 'bundle:path_provider/path_provider.dart';
import 'bundle:permission_handler/permission_handler.dart';
<uses-permission android:title="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:title="android.permission.WRITE_EXTERNAL_STORAGE"/>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments