【问题标题】:Flutter: type 'Future<dynamic>' is not a subtype of type 'Widget'Flutter:类型“Future<dynamic>”不是“Widget”类型的子类型
【发布时间】:2019-08-18 02:41:39
【问题描述】:

我正在使用 sqlite 在我的 Flutter 应用程序中存储数据。我有一个模态底页,它打开过滤器芯片,当您选择一个时,它会将项目添加到数据库中。如果该项目已存在,则检查过滤芯片。当我调用数据库函数检查项目是否已经存在时,我收到以下错误。

我尝试过同时使用 async 和 await。

数据库查询代码:

// FIND TAG
findTag(int tagId) async {
    var dbConnection = await db;
    var res = await  dbConnection.query("$TABLE_NAME", where: "tagId = ?", whereArgs: [tagId]);
    return res.isNotEmpty ? true : false ;
  }

模态底页小部件容器代码:

Widget build(BuildContext context) {
    setState(() {
      _index = widget.index;
      _list =widget.list;
    });

    return new Container(
      padding: new EdgeInsets.all(27.0),
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,              
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget> [
          new Text(_list[_index]["title"], style: new TextStyle(  fontWeight: FontWeight.bold, fontSize: 22),),          
          new Text(_list[_index]["description"]),
          getFilterChipsWidgets(),
        ],
      ),
    );

}


getFilterChipsWidgets()
  async {

    List<Widget> tagsList = new List<Widget>();
      for(var i=0; i<_list[_index]["tags"].length; i++) {       
        var dbHelper = DBHelper();
        int id = int.parse(_list[_index]["tags"][i]["id"]);

        var exist = await dbHelper.findTag(id);
        FilterChip item = new FilterChip(
          label: Text(_list[_index]["tags"][i]["name"].toString(),), 
          selected: exist,
          onSelected: (bool newValue) {
            if(newValue) {
              dbHelper.addNewTag(id);
            } else {
              dbHelper.deleteNewTag(id);
            }
          },
        );
        tagsList.add(item);       
      }

       return Wrap(
          spacing: 8.0, // gap between adjacent chips
          runSpacing: 4.0, // gap between lines
          children: tagsList,
        );
}


【问题讨论】:

    标签: sqlite dart flutter


    【解决方案1】:

    您的 getFilterChipsWidgets() 是异步函数,因此它将返回 Future。 您可以等待未来并将小部件保存到列表并在完成后调用 setState。 或者像这样用 FutureBuilder 包装它:

        children: <Widget> [
          new Text(_list[_index]["title"], style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 22),),          
          new Text(_list[_index]["description"]),
          FutureBuilder<Widget>(
           future: getFilterChipsWidgets,
           builder: (BuildContext context, AsyncSnapshot<Widget> snapshot){
             if(snapshot.hasData)
               return snapshot.data;
    
             return Container(child: CircularProgressIndicator());
           }
          ),
        ],
    

    【讨论】:

    • 最近一小时我一直在寻找这个。非常感谢!
    【解决方案2】:

    有时还可以看到,您错误地分配了函数async,并且在函数块内没有您正在使用await 进程的位置,

    类似的情况已经发生在我身上,通过在函数上删除这个未使用的async 解决了上述问题。

    当你创建任何函数async 时,函数请求者会以Future&lt;T&gt; 的方式处理它,所以要小心。

    【讨论】:

    • 我的流构建器需要 Stream&lt;QuerySnapshot&lt;Object?&gt;&gt; 类型数据,而它正在返回 Future 类型数据并在控制台中出现错误“类型 'Future' 不是类型 'Stream >?'”请建议如何解决它。谢谢。
    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2020-09-11
    • 2021-02-17
    • 2023-03-27
    • 1970-01-01
    • 2021-02-10
    • 2020-04-07
    相关资源
    最近更新 更多