【问题标题】:Type 'Future<dynamic>' is not subtype of type 'Widget''Future<dynamic>' 类型不是 'Widget' 类型的子类型
【发布时间】:2022-01-04 06:41:47
【问题描述】:

我在谷歌地图上显示来自 API 的标记。这是我的构建方法。当程序到达 _widgetbuilder() 方法时,它会抛出 Future is not a subtype of widget 类型的特定错误。如果有人可以帮助解决问题,并告诉我这个错误到底意味着什么.....

Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.white,
  body: FutureBuilder<List<MarkersOnMap>>(
    future: future,
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      if (!snapshot.hasData)
        return Container(
          child: Center(
            child: CircularProgressIndicator(),
          ),
        );
      if (snapshot.hasData && snapshot.data.isEmpty) {
        return Center(
          child: Container(
            child: Column(
              children: [
                Text(
                  'No Properties Added Yet\nPlease Add Some!',
                  style:
                      TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
                ),
                ElevatedButton.icon(
                  onPressed: () {
                    Navigator.push(
                      context,
                      PageTransition(
                        duration: Duration(microseconds: 500),
                        type: PageTransitionType.fade,
                        child: AddNewEproperty(
                            createEproperty: widget.createEproperty),
                      ),
                    );
                  },
                  label: Text('Add'),
                  icon: Icon(Icons.add),
                ),
              ],
            ),
          ),
        );
      } else
        _widgetbuilder();

      if (snapshot.hasData) {
        return ListView.builder(
          itemCount: allWidgets.length + 1,
          shrinkWrap: true,
          padding: EdgeInsets.only(top: 16),
          physics: NeverScrollableScrollPhysics(),
          itemBuilder: (context, i) {
            return Stack(
              children: <Widget>[
                Container(),],);},);},},),);}

这是 _widgetbuilder() 方法。当它达到这个返回 _widgetbuilder 时,抛出 _typeerror。

    _widgetbuilder() async {
    allWidgets = [];
    widget.markersonmap = await future;
    
    widget.markersonmap.forEach(
      (element) {
        print(element);
        allWidgets.add(
          Container(
            height: 25,
            width: 50,
            child: new DecoratedBox(
              decoration: BoxDecoration(
                  border: Border.all(color: Colors.black),
                  borderRadius: BorderRadius.circular(5.0),
                  color: Colors.black54),
              child: Text(
                element.ePropertiesCardsList.price.toString(),
                textAlign: TextAlign.center,
                style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                    color: Colors.white),
              ),
            ),
          ),
        );
      },
    );
  }

【问题讨论】:

    标签: flutter widget subtype


    【解决方案1】:

    您收到此错误是因为您的函数 _widgetbuilder 返回 Future&lt;dynamic&gt;,因为该函数是异步的。

    Widget _widgetbuilder(){
    // code here
    }
    

    函数应该在这个结构中才能返回类型为Widget。如果您确实需要,应该将需要异步的代码从构建函数中取出,或者使用.then 模式作为异步代码而不是异步等待。 This short 9 min video 将帮助你更好地理解 Flutter 中的异步。

    【讨论】:

    • 这很有帮助。非常感谢先生!我想投票给你的答案,但他们不允许我投票。 :{
    • 很高兴我能帮上忙。如果对您有帮助,您可以将其标记为正确答案。仅当声誉超过 15 时才允许投票。
    • 我已应用此代码 future.then((value) { widget.markersonmap = value; });而不是 widget.markersonmap = 等待未来;但是当它到达 .then 时,它不会从我的 api 方法 future 获取数据,即 getdata(){};
    • 您获得了什么价值?如果您没有得到任何有价值的东西,那么您可能需要重新检查和调试您的获取功能。
    • 以前当我使用 async-await 时,能够跳转到 fetching 函数,但现在在 .then() 方法中,程序在到达 .then() 方法时它不会跳转到 fetching方法。
    【解决方案2】:

    现在在这里类型错误已解决,但在读取 'future.then..... 它不会转到未来并获取数据,而是跳转到下一个 foreach 行,然后将其调用为 null。

    _widgetbuilder() {
    allWidgets = [];
    // widget.markersonmap = await future;
    
    future.then((value) {
      widget.markersonmap = value;
    });
    widget.markersonmap.forEach(
          (element) {
            print(element);
            allWidgets.add(
              Container(
    // other code
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 2023-03-27
      • 2019-08-18
      • 2023-03-30
      • 1970-01-01
      • 2021-02-17
      • 2021-10-21
      • 2020-09-24
      相关资源
      最近更新 更多