【问题标题】:Flutter type 'List<Future<Widget>>' is not a subtype of type 'List<Widget>'Flutter 类型“List<Future<Widget>>”不是“List<Widget>”类型的子类型
【发布时间】:2020-04-07 14:07:46
【问题描述】:

我收到此错误: type 'List>' 不是 type 'List' 的子类型

这是触发错误的代码。

@override
  Widget build(BuildContext context) {
    if (widget.productId != null) {
      return StreamBuilder(
          stream:
              Document<Product>(path: 'app-data-inventory/${widget.productId}')
                  .streamData(),
          builder: (BuildContext context, AsyncSnapshot snap) {
            if (snap.hasError) {
              print(snap.error);
            }
            if (snap.hasData) {
              Product product = snap.data;

              return Scaffold(
                resizeToAvoidBottomPadding: false,
                backgroundColor: AppAppTheme.white,
                appBar: appBarComponents,
                key: _scaffoldKey,
                body: Stack(
                  children: <Widget>[
                       Builder(
                            builder: (context) => SingleChildScrollView(
                              child: Column(
                                  crossAxisAlignment:
                                      CrossAxisAlignment.stretch,
                                  mainAxisSize: MainAxisSize.min,
                                  children: [
                                    Container(
                                      color: AppAppTheme.primary,
                                      height: 230,
                                      child: _yourWidget(context, product.productStorage['paths']),
                                    ),
                                    SizedBox(
                                      height: 10,
                                    ),
                                    CheckboxListTile(
                                        title: const Text('Terms of services'),
                                        value: _product.termsAgreed != null
                                            ? _product.termsAgreed
                                            : false,
                                        onChanged: (val) {
                                          setState(
                                              () => _product.termsAgreed = val);
                                        }),
                                    SwitchListTile(
                                        title: const Text('Save as draft'),
                                        value: _product.productStatus == 'draft'
                                            ? true
                                            : false,
                                        onChanged: (bool val) => setState(() {
                                              _product.productStatus =
                                                  val ? 'draft' : 'unapproved';
                                            })),
                                  ]),
                            ),
                          )

                    ),
                  ],
                ),
                  ),
                ),
              );
            } else {
              return UIErrorsMessages.notFoundComponent(
                  context, 'Product not found!');
            }
          });
    } else {
      return UIErrorsMessages.somethingIsNotRightComponent(
          context, 'Something went wrong. Try again!');
    }
  }
}

我该如何解决这个问题?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您应该首先使用FutureBuilder 获取数据,然后将数据提供给您的小部件。

    以下代码应该适合你:

    (我没有完整的代码所以我没有测试过,如有任何问题请评论)

    Widget _yourWidget(BuildContext context) {
        return 
              color: AppTheme.primary,
              height: 230,
              child: Wrap(
                children: 
                paths.map<Widget>((url) {
                  return FutureBuilder(builder: (BuildContext context, AsyncSnapshot snapshot) {
                    if (snapshot.hasData){
                      file = snapshot.data;
                      print(file);
                      return Column(
                        children: <Widget>[
                          FlatButton(
                            child: Text(
                              'Delete',
                              style: AppTheme
                                  .titleWhite,
                            ),
                            onPressed: () {
                              print(url);
                            },
                          ),
                          Container(
                            width: 100.0,
                            height: 100.0,
                            margin:
                                EdgeInsets
                                    .all(
                                        10),
                            color:
                                AppTheme
                                    .warn,
                            child: ExtendedImage
                                .network(file ==
                                        null
                                    ? '/assets/icons/icon-120x120.png'
                                    : file),
                          )
                        ],
                      );
                    }else{
                      return Center(child: CircularProgressIndicator());
                    }
                  },);
           });
      }
    
      Future fetchDownloadURL(){
        return await _storage
                   .ref()
                 .child(url)
                 .getDownloadURL();
      }
    

    【讨论】:

    • 谢谢。但是仅仅打电话给future: fetchDownloadURL() 是没有意义的。鉴于它需要一个参数 (url) 才能返回 downloadURL。 url 在数组调用路径中。因此,在将参数传递给future: fetchDownloadURL(), 之前,需要执行某种类型的循环来获取每个 url。除非我弄错了??
    • 将 FutureBuilder 包裹在 Column Widget 周围,并将参数中的 url 提供给 fetchDownloadURL() ,它应该适合你。
    • 我有这样的事情:Stack( children: &lt;Widget&gt;[ Builder( builder: (context) =&gt; SingleChildScrollView( child: Column( children: [ Container( child: _yourWidget(context, paths) 但我确实在fetchDownloadUrl() 上得到了 url not defined 错误。可能我没明白你的意思。
    • 一切都是正确的,如果你想在你的小部件树中使用 Future,那么你需要使用 FutureBuilder。在您的代码中,您在列小部件上使用 Future,因为您需要那里的数据。现在用 FutureBuilder 包装那个小部件,当你收到数据时将它传递给 Column 直到显示加载小部件或类似的东西。
    • 谢谢。我将更新我的问题代码。希望您能更好地指导我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2018-09-11
    相关资源
    最近更新 更多