简体   繁体   English

为什么 dart 泛型类不能根据构造函数确定其类型

[英]Why can't a dart generic class determine its type based on constructor

I am writing my own MyFutureBuilder for my FutureBuilders do the same thing every time (except working with the snapshot data for which I specify my separate function).我正在为我的 FutureBuilders 编写自己的 MyFutureBuilder,每次都做同样的事情(除了使用我为其指定单独函数的快照数据)。

typedef MyFutureBuilderBuildFunction<T> = Widget Function(T snapshotData);

class MyFutureBuilder<T> extends StatefulWidget {
  final MyFutureBuilderBuildFunction<T> builder;
  final Future<T> future;

  MyFutureBuilder({this.future, @required this.builder})
      : assert(builder != null);

  @override
  State<StatefulWidget> createState() {
    return MyFutureBuilderState(future: future, builder: builder);
  }
}

class MyFutureBuilderState<T> extends State<MyFutureBuilder<T>> {
  MyFutureBuilderBuildFunction<T> builder;
  Future<T> future;

  MyFutureBuilderState({this.future, @required this.builder})
      : assert(builder != null);

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<T>(
      future: future,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return builder(snapshot.data);
        } else if (snapshot.hasError) {
          return Text('${snapshot.error}');
        }

        return Center(child: CircularProgressIndicator());
      },
    );
  }
}

When I instantiate it with当我实例化它时

Future<Article> _article;
MyFutureBuilder(
    future: _article,
    builder: (snapshotData) {

the type of snapshotData is dynamic and not Article . snapshotData 的类型是动态的,而不是Article Can't the type T be automatically be determined from the type of _article, Future?不能从_article,Future的类型中自动确定类型T吗?

When I seperately give the type to MyFutureBuilder (which is IMHO redundant)当我将类型单独提供给 MyFutureBuilder 时(恕我直言,这是多余的)

Future<Article> _article;
MyFutureBuilder<Article>(
    future: _article,
    builder: (snapshotData) {

the type of snapshotData is of course Article. snapshotData 的类型当然是文章。

In 1st code You should cast the returned snapshotData to the type you need before using it在第一个代码中,您应该在使用之前将返回的 snapshotData 转换为您需要的类型

Future<Article> _article;
MyFutureBuilder(
  future: _article,
  builder: (snapshotData) {
    Article article = snapshotData as Article;
  }
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能扩展接口“泛型方法”并将其类型缩小到继承的接口“类通用”? - Why can't I extend an interface “generic method” and narrow its type to my inherited interface “class generic”? 重载泛型类的构造函数,基于类型 - Overload the Constructor of the generic class, Based on Type 为什么不能将包含通用类型的通用类型分配给通配符类型的通用类型的类 - Why can't a Generic type containing a Generic type be assigned to a Generic typed class of wildcard type 通用类 <T> 用构造函数选择类型 - Generic class<T> with constructor to choose the type 是否可以从其构造函数的参数推断出类的泛型类型参数? - Can a class's generic type parameters be inferred from the arguments of its constructor? 为什么我不能创建泛型类型的内部类的数组? - Why can't I create an array of an inner class of a generic type? 为什么我不能在 Kotlin 中使用 Class 类型作为泛型参数? - Why I can't use a Class type as a generic parameter in Kotlin? Dart:如何在运行时确定可为空的泛型类型? - Dart: how to determine nullable generic type at runtime? 为什么不在用作泛型类型参数的类上调用静态构造函数? - Why isn't a static constructor invoked on a class used as a generic type parameter? 我可以强制构造函数对其泛型类型设置更严格的限制吗? - Can I force a constructor to put a stricter bound on its generic type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM