Flutter/Dart 异步不等待

Flutter/Dart Async Not Waiting

我正在构建我的第一个 Flutter 应用程序,我 运行 遇到了一些异步问题。

当我的应用程序执行时,我希望它请求权限并等待它们被授予。我的 main() 函数如下所示:

import 'permission_manager.dart' as Perm_Manager;

void main() async
{
  //Ensure valid permissions
  Perm_Manager.Permission_Manager pm = Perm_Manager.Permission_Manager();
  var res = await pm.get_permissions();
  print(res);

  return runApp(MyApp());
} 

Permission Manager class' get_permissions() 函数使用 Flutter Simple Permissions 包来检查和请求权限。

import 'package:simple_permissions/simple_permissions.dart';
import 'dart:io' as IO;
import 'dart:async';

class Permission_Manager {
  /* Get user permissions */

  Future<bool> get_permissions() async
  {
    //Android handler
    if (IO.Platform.isAndroid)
    {
        //Check for read permissions
        SimplePermissions.checkPermission(Permission.ReadExternalStorage).then((result)
        {
          //If granted
          if (result)
            return true;

          //Otherwise request them
          else
          {
            SimplePermissions.requestPermission(Permission.ReadExternalStorage)
            .then((result)
            {
              // Determine if they were granted
              if (result == PermissionStatus.authorized)
                return true;
              else
                IO.exit(0); //TODO - display a message
            });
          }
        });
    }

    else
      return true;
  }
}

当我 运行 应用程序时,它不会等待功能按预期完成,而是在更新 Future 之前打印 "res" 的值。

Launching lib\main.dart on Android SDK built for x86 in debug mode...
Built build\app\outputs\apk\debug\app-debug.apk.
I/SimplePermission(15066): Checking permission : android.permission.READ_EXTERNAL_STORAGE
I/flutter (15066): null
I/SimplePermission(15066): Requesting permission : android.permission.READ_EXTERNAL_STORAGE

The Future returns 函数中途的一个值!有谁知道我做错了什么?

要等待某事,您必须在未来调用 await 关键字而不是 .then

final result = await future;
// do something

而不是

future.then((result) {
  // do something
});

如果你真的想要使用.then那么你可以等待生成的未来:

await future.then((result) {
  // do something
});

只需确保在使用嵌套异步调用时,每个调用都使用了 async 关键字:

await future.then((result) async{
    // do something
    await future.then((result_2) {
       // do something else
    });
});

成功了。该问题似乎已使用完成者解决:

import 'package:simple_permissions/simple_permissions.dart';
import 'dart:io' as IO;
import 'dart:async';

class Permission_Manager {
  /* Get user permissions */

  final Completer c = new Completer();

  Future get_permissions() async
  {

    //Android handler
    if (IO.Platform.isAndroid)
    {
        //Check for read permissions
        SimplePermissions.checkPermission(Permission.ReadExternalStorage).then((result)
        {
          //If granted
          if (result)
          {
            c.complete(true);
          }
          //Otherwise request them
          else
          {
            SimplePermissions.requestPermission(Permission.ReadExternalStorage).then((result)
            {
              // Determine if they were granted
              if (result == PermissionStatus.authorized)
              {
                c.complete(true);
              }
              else
              {
                IO.exit(0); //TODO - display a message
              }
            });
          }
        });
    }

    else
    {
      c.complete(true);
    }

    return c.future;
  }

}