Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use generic function in a generic function

Tags:

flutter

dart

I have the following class in my code

abstract class DatabaseKey<T> implements Built<DatabaseKey<T>, DatabaseKeyBuilder<T>> {
  DatabaseKey._();
  factory DatabaseKey([void Function(DatabaseKeyBuilder<T>) updates]) = _$DatabaseKey<T>;

  String get name;
}

Then, I define the following generic typedef function:

typedef ObserveDatabaseEntity = Observable<DatabaseEntity<T>> Function<T>(DatabaseKey<T> key);

But, when I try to use it as follows, the code has an error.

  static ObserveConfigurationValue observe(
      GetConfigurationState getState,
      ObserveDatabaseEntity observeDatabaseEntity,
  ) {
    assert(getState != null);
    assert(observeDatabaseEntity != null);

    return <KT>(ConfigKey<KT> key) {
      return Observable.just(getState())
          .flatMap((state) {
            final dbKey = _databaseKeyFromConfig<KT>(key);

            return observeDatabaseEntity(dbKey)
              .map(_configValueFromDatabaseEntity);
          });
    }
  }
DatabaseKey<T> _databaseKeyFromConfig<T>(ConfigKey<T> key) {
  return DatabaseKey((build) => build
    ..name = key.value,
  );
}

The error I am getting is:

The argument type DatabaseKey can't be assigned to the parameter DatabaseKey.

I see nothing wrong with this code or why it shouldn't work, but maybe my understanding of what can be written in Dart is wrong. What would be the correct way to write this, if possible at all?

EDIT#1:

Note:

The typedef ObserveDatabaseEntity is in one file The static ObserveConfigurationValue observe(GetConfigurationState getState, ObserveDatabaseEntity observeDatabaseEntity) is is another file

From playing around, it seems that placing them in a single file, the error disappears.

Still, I believe that this should work in separate files as well,

like image 276
filipproch Avatar asked Nov 07 '22 15:11

filipproch


1 Answers

This error looks like an import mismatch.

In dart, you can import file either through relative path or package.

import 'lib/some_file.dart'; //relative
import 'package:myapp/lib/some_file.dart'; //package

There's really no better way but once you choose one, you have to stick to it. If you don't (meaning you have imported a file using a package import and the same file elsewhere with a relative path) Dart will place them in two different namespaces and think they are two different classes.

like image 72
Muldec Avatar answered Nov 15 '22 07:11

Muldec