title: Flutter health permissions
body: <p>I use the <a href="https://pub.dev/packages/health" rel="nofollow noreferrer">health</a> package in my App to sync data between my app and Google Fit / Apple Health. So far I have only testet Apple Health and it works perfectly fine if I grant all the necessary permissions. But I found one weird thing and I can't figure out how to fix it. So far I have only implemented and tested the integration with Apple Health.</p>
  <p>So, I check if the app has the permission to read or write a certain data type, and then I read it or write it to Apple Health. If the permission was granted and the read / write operation was successful, I want to present that to the user. If the permission was not granted, I also want to present that to the user, so he knows what went wrong. In the <code>HealthFactory</code> class of the package there is the <code>requestAuthorization</code> method that I call to check whether the user has permission or not. The behavior I expected was that, if the permission is not granted, the return value of that method is <code>false</code> if the permission is not granted. But instead, it is always <code>true</code>. Also, the methods <code>getHealthDataFromTypes</code> and <code>writeHealthData</code> don't really indicate whether the permission is granted or not.</p>
  <p>I implemented a few methods to showcase that:</p>
  <pre><code>class TestScreen extends StatelessWidget {
    TestScreen({Key? key}) : super(key: key);
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ElevatedButton(
                child: Text(&quot;Health Permission&quot;),
                onPressed: () async {
                  final HealthFactory _health = HealthFactory();
                  await Permission.activityRecognition.request();
                  bool permission = await _health.requestAuthorization(
                    [HealthDataType.WEIGHT],
                    permissions: [HealthDataAccess.READ_WRITE],
                  );
                  print(permission);
                },
              ),
              ElevatedButton(
                child: Text(&quot;Read Data&quot;),
                onPressed: () async {
                  final HealthFactory _health = HealthFactory();
                  DateTime untilDate = DateTime.now();
                  DateTime fromDate = DateTime(1970);
                  try {
                    List&lt;HealthDataPoint&gt; healthData =
                        await _health.getHealthDataFromTypes(
                      fromDate,
                      untilDate,
                      [HealthDataType.WEIGHT],
                    );
                    return print(healthData.toString());
                  } on Exception catch (e) {
                    print(e.toString());
                    rethrow;
                  }
                },
              ),
              ElevatedButton(
                child: Text(&quot;Write Data&quot;),
                onPressed: () async {
                  final HealthFactory _health = HealthFactory();
                  try {
                    bool success = await _health.writeHealthData(
                      80,
                      HealthDataType.WEIGHT,
                      DateTime.now(),
                      DateTime.now(),
                    );
                    print(success);
                  } on Exception catch (e) {
                    print(e.toString());
                  }
                },
              ),
            ],
          ),
        ),
      );
    }
  }
  </code></pre>
  <p>This is my test screen.</p>
  <p>The first button requests READ_WRITE permission for the datatype WEIGHT. At the first call, this window pops up:
  <a href="https://i.stack.imgur.com/ShsIe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ShsIe.png" alt="Health Access" /></a>
  When I click on &quot;Don't Allow&quot; in the top left corner, the permissions are NOT granted, but the method still returns <code>true</code>. If I go to the settings, no matter if I activate the permission or not, it always returns <code>true</code>.</p>
  <p>The second button is supposed to read WEIGHT data from Apple Health. If the permission is granted, everything works fine. If the permission is NOT granted, it still &quot;works&quot;, but jsut returns an empty list. This is pretty bad, because I can't indicate whether it failed or Apple Health just doesn't have any WEIGHT data.</p>
  <p>The third button is supposed to write WEIGHT data into Apple Health. If the permission is granted, again, everthing works fine. If ther permission is NOT granted, the mehod returns false and doesn't write anything. This is already a better indicator than from the READ operation, but still, I can't indicate whether the permission wasn't granted or it was some other error.</p>
  <p>So my question is: Is there any way to indicate at code level whether the permissions have been granted or not? I really need this to present to the user if the sync worked, and if not, why it didn't work.</p>