0

my code ->

import 'package:app_usage/app_usage.dart';
    
class ScreenTimeService {
  Future<List<AppUsageInfo>> getUsageStats() async {
    try {
      DateTime endDate = DateTime.now();
      DateTime startDate = endDate.subtract(const Duration(days: 1));
          List<AppUsageInfo> infoList = await AppUsage().getAppUsage(startDate, endDate);
          infoList.removeWhere((app) => app.usage.inSeconds == 0);
          return infoList;
        } on AppUsageException catch (exception) {
          print(exception);
          // You should guide the user to the settings here
          // AppUsage().openUsageSettings();
          return [];
        }
      }
    }

The full codebase it logically and syntax correct but I'm getting this error

The name 'AppUsageException' isn't a type and can't be used in an on-catch clause. Try correcting the name to match an existing class.

1 Answer 1

0

I searched through the package code and AppUsageException is not a defined type. So, on the home page of the package, it might be a mistake. In all the source code, there is no definition for AppUsageException.

So, try removing it and handle exceptions like this:

import 'package:app_usage/app_usage.dart';

class ScreenTimeService {
  Future<List<AppUsageInfo>> getUsageStats() async {
    try {
      DateTime endDate = DateTime.now();
      DateTime startDate = endDate.subtract(const Duration(days: 1));
      List<AppUsageInfo> infoList = await AppUsage().getAppUsage(startDate, endDate);
      infoList.removeWhere((app) => app.usage.inSeconds == 0);
      return infoList;
    } catch (e) {
      print(e);
      return [];
    }
  }
}

This approach is also used in the example code provided by the package.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.