Flutter Future<dynamic> vs. Future<String> 子类型错误?

Flutter Future<dynamic> vs. Future<String> subtype error?

我刚刚更新了 Flutter 并从 git 成功下载了我的原始项目。现在我收到一个奇怪的 Future 错误。我在 github 上看到在线提到它,但没有关于如何修复的明确答案。该项目甚至不加载。它从我的 main.dart 文件和 returns 中读取 Future 语句...

[VERBOSE-2:dart_error.cc(16)] Unhandled exception: type 'Future dynamic' is not a subtype of type 'Future String' where
Future is from dart:async
Future is from dart:async
String is from dart:core

*** 不确定错误在哪里。我的飞镖分析说 "Await Only Futures"。这是我的期货代码,我 运行 在我的小部件开始构建之前...

Future<Null> getData() async{
    FirebaseUser user = await FirebaseAuth.instance.currentUser;
    user = FirebaseAuth.instance.currentUser;
    var userid = user.uid;
    fb.child('users/${userid}').onValue.listen((Event event) {
      if (event.snapshot.value != null) {
        name = event.snapshot.value['displayName'];
        image = event.snapshot.value['image'];
      } else {
        name = "User";
      }
    });
  }

好的。我想我明白了。

Dart 1.0,是一种软类型语言,所以类似于

main() async {  
  print(await getData()); 
}

Future<Null> getDatare() async{return "a";} 

将打印 "a"

问题在于 Dart 2.0 是一种类型化语言,因此 Future 实际上是有意义的。这里发生的事情(简而言之)是 Future 变成了 FutureNull,并且您无法从 FutureNull 中获取字符串(因为它只包含一个 Null)。

其实,这种事情曾经让我有点头疼[1],但仔细想想,还是有道理的。看下面的代码:

List<dynamic> l = ["a", "b", "c", "d"];
List<String> d = l;

甚至更多

List<int> l = [1,2,3,4];
List<double> d = l;

在 Flutter 中会崩溃。为什么?因为想想这里发生了什么:

什么是 "l"?

----------------------
| int | int | int | int |
----------------------

什么是 "d"?

---------------------------------------------
| double | double | double | double |
---------------------------------------------

那么"l"和"d"如何转换呢?

你必须创建一个新的双精度列表,然后复制 l 中的值,将它 转换为双精度,然后将它存储在 "d".

但这不仅仅适用于列表。它适用于所有泛型。

当你有类似 A<B> 的东西时,它是 A<C> 完全不同的类型,你不能简单地从一个转换到其他,同理:

取以下代码:

class Box<T> {
  T a;
  Box(T v) {
    a=v;
  }
  T getVal() {
    return a;
  }
  }

现在将 Box 转换为 Box。没有道理,对吧?我可以执行以下操作吗?

Box<int> i = new Box(5);
Box<String> s= new Box("a");
i + (s as Box<int>)

所以这里真正发生的是 Box 变为 BoxInt,Box 变为 BoxString(编译器 find/replacing 标识符 "T" 与 "int" 或 "T" 与 "String").

所以你在这里遇到了同样的事情:你必须从未来拆箱,然后使用它。在你的情况下(实际上,你的情况还有一个额外的问题 - 你没有返回任何东西)你可能想要一个 Future 然后 await 那个 Future 并得到一个 String.


[1]。它怎么咬我的?来自 Kotlin(或任何其他 JVM 语言),我习惯于能够做类似的事情:

List<A> a = ...;
List<B> b = a;

没有问题。为什么?因为 JVM 没有真正的泛型。它所做的叫做"type erasure",真正发生的事情是:

List<Object> a = ...;
List<Object> b = a;

好吧,这显然有效,因为所有 List 都有一个指向对象的指针,在 运行 时间确定。

那是因为 Java 1.0(与 Dart 不同)从来没有泛型,它们是固定的,所以为了向后兼容,它们使泛型的类型安全性低于它们本来可以拥有的。

所以 Dart(或 C++ 或 Rust 或 Go 数组)将泛型视为单态的,而 Java 将它们视为多态代码(将 "T" 视为指针)。