Asynchronous function returns null

The dayManager.getDay() returns a null instead of a Map

@override
void initState() {
    super.initState();
    print("init");
    getDay();
  }

  void getDay() async {
    Future<Map<int, int>> dayExistingValueMap = dayManager.getDay();
    dayExistingValueMap.then((map) {
      print('dayExisting ____ $map');
    });
  }

but when I print it out beforee returning it, the Map contains all the values.

    Future<Map<int, int>> getDay() async {
    final Future<Database> dbFuture = databaseHelper.initializeDatabase();
    dbFuture.then((database) async {
      final bool checkIfExists = await databaseHelper.checkIfExist(day.dateStr);
      if (checkIfExists) {
        Day dbDay = await databaseHelper.getDaySingle(day.dateStr);
        print('_${dbDay.valueMap}');
        return dbDay.valueMap;
      } else {
        return this.day.valueMap;
      }
    });
  }

Your function is returning null since you're not actually returning anything. You need to return the result of dbFuture.then, like so:

    Future<Map<int, int>> getDay() async {
      final Future<Database> dbFuture = databaseHelper.initializeDatabase();
      return dbFuture.then((database) async {
        final bool checkIfExists = await databaseHelper.checkIfExist(day.dateStr);
        if (checkIfExists) {
          Day dbDay = await databaseHelper.getDaySingle(day.dateStr);
          print('_${dbDay.valueMap}');
          return dbDay.valueMap;
        } else {
          return this.day.valueMap;
        }
      });
    }

Future.then returns a Future that completes to the return value of the closure provided.

Return null from async/await function � Issue #23275 � dotnet/roslyn , Version Used: 11152017 Steps to Reproduce: private async Task foo() { await Task.Delay(1000); return null; // CS8600 } Expected Behavior:� Async Return Types (C#) 04/14/2020; 8 minutes to read +8; In this article. Async methods can have the following return types: Task<TResult>, for an async method that returns a value. Task, for an async method that performs an operation but returns no value. void, for an event handler. Starting with C# 7.0, any type that has an accessible

Instead of return dbFuture.then((database) async { we can use final Database dbFuture = await databaseHelper.initializeDatabase();

Future<Map<int, int>> getDay() async {
    final Database dbFuture = await databaseHelper.initializeDatabase();

      final bool checkIfExists = await databaseHelper.checkIfExist(day.dateStr);
      if (checkIfExists) {
        Day dbDay = await databaseHelper.getDaySingle(day.dateStr);
        print('_${dbDay.valueMap}');
        return dbDay.valueMap;
      } else {
        return this.day.valueMap;
      }

  }

async function, Task<TResult>, for an async method that returns a value. void , for an ReadLineAsync(); while (line != null) { foreach (string word in line. The order of these modifiers is not enforced by the C# compiler, but generally, as a convention, the async modifier is put last, just before the return type, which is Task in this example. It's because of this typical positioning that often an asynchronous method in C# is referred to as being either async void or async Task. The contrast

You are returning the value within the closure inside the then, so as far as the getDay method is concerned, you aren't returning anything. My recommendation is to not use then at all, since it directly causes these kinds of confusions. (Not to mention calling then defeats the purpose of marking your method as async which is to be able to use await).

Future<Map<int, int>> getDay() async {
  final database = await databaseHelper.initializeDatabase();
  final bool checkIfExists = await databaseHelper.checkIfExist(day.dateStr);
  if (checkIfExists) {
    Day dbDay = await databaseHelper.getDaySingle(day.dateStr);
    print('_${dbDay.valueMap}');
    return dbDay.valueMap;
  } else {
    return this.day.valueMap;
  }
}

Async return types (C#), Returning null from a non- async Task / Task<T> method will cause a NullReferenceException at runtime. This problem can be avoided by returning Task. your function getData will return a Promise. So you can either: await the function as well to get the result. However, to be able to use await, you need to be in an async function, so you need to 'wrap' this: async function callAsync() { var x = await getData(); console.log(x); } callAsync();

C#: Non-async "Task/Task<T>" methods should not return null, What's more, changing the return type of an async method can be would return an html string retrieved from a remote server, or null if no url� Probably the simplest way to do it is to create a delegate and then BeginInvoke, followed by a wait at some time in the future, and an EndInvoke.. public bool Foo(){ Thread.Sleep(100000); // Do work return true; } public SomeMethod() { var fooCaller = new Func<bool>(Foo); // Call the method asynchronously var asyncResult = fooCaller.BeginInvoke(null, null); // Potentially do other work while

An Overview of Async Method Return Types in C#, No it's not returning immediately. The await makes the method call asynchronous . When FindAsync is called, the Details method returns with a task which is not� async functions let you write Promise-based code as if it were synchronous. Once you define a function using the async keyword, then you can use the await keyword within the function’s body. When the async function is called, it returns with a Promise. When the async function returns a value, the Promise gets fulfilled, if the async function

Why would you ever 'await' a method, and then immediately , AVOID returning null for void. In a large variety of languages void as return type is used to indicate that a function doesn't return anything. Dart allows returning� Controller action return types in ASP.NET Core web API. 02/03/2020; 6 minutes to read +2; In this article. By Scott Addie. View or download sample code (how to download). ASP.NET Core offers the following options for web API controller action return types:

Comments
  • try this Future<Map<int, int>> dayExistingValueMap = await dayManager.getDay();
  • Technically correct, but this method invalidates the purpose of async.
  • I wouldn't say "invalidates" is the right word, but you're right that it would be cleaner to make full use of async-await. Regardless, forgetting to actually return from a method will always result in its result being null.
  • Maybe not "invalidates" as much as "makes redundant". The outer method is marked as async but it never awaits anything and it explicitly returns a future. Using async-await would result in cleaner code, but if nothing else the getDay method here has no reason to be marked as async. (Personally, I don't know why explicitly returning a future in an async method isn't marked as a warning by the dart linter.)