【问题标题】:Flutter FutureBuilder couldn't get future from methodFlutter FutureBuilder 无法从方法中获得未来
【发布时间】:2019-08-21 14:55:13
【问题描述】:

在颤振应用程序中,我实现了简单的搜索视图,用户可以从网络服务器搜索单词,我的 http 调用方法和获取数据,我可以在 console 上打印结果,但我不能显示到listview.

可布局:

Container(
  height: 70.0,
  child: Row(
    children: <Widget>[
      Flexible(
        flex: 1,
        child: Padding(
          padding: const EdgeInsets.only(left: 8.0),
          child: TextField(
              controller: _searchController,
              textAlign: TextAlign.start,
              keyboardType: TextInputType.text),
              textInputAction: TextInputAction.send,),
        ),
      ),
      InkWell(
        child: Container(
          height: 40.0,
          width: 40.0,
          child: Icon(Icons.send, size: 25.0, color: Colors.black),
        ),
        onTap:_search,
      ),
    ],
  ),
),
Expanded(
  child: FutureBuilder(
      future: _searchPost(),
      builder: (context, snapshot) {
        if(snapshot.hasData) {
          List<Contents> content = snapshot.data;
          ListView.separated(
            itemBuilder: (context, index) {
              return ListTile(
                title: Text(content[index].title),
                ),
                subtitle: Text(content[index].createdAt),
                ),
              );
            },
            itemCount: content.length,
            separatorBuilder: (context, index) {
              return Divider(height: 1.0);
            },
          );
        }else if (snapshot.hasError){
          print('error);
        }

        return Center(
          child: CircularProgressIndicator(
            valueColor: AlwaysStoppedAnimation(Colors.blue),
          ),
        );
      }),
)

http调用方式:

  Future<List<Contents>> _searchPost() async {
    final response = await http.get('${Constants.searchPosts}${_searchController.text}').timeout(Duration(seconds: 1));
    if(response.statusCode == 200){
      final responseString = json.decode(response.body) as List;
      List<Contents> res= responseString.map((j)=>Contents.fromJson(j)).toList();

      print(json.decode(response.body));//<-- work fine and show result in console
      return res;
    }else{
      return null;
    }
  }

点击离子按钮进行搜索:

  void _search() {
    if (_searchController.text.length <= 0) {
    }else{
      _searchPost();
    }
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您在第一个 if 中缺少 return 语句

      if(snapshot.hasData) {
              List<Contents> content = snapshot.data;
              return ListView.separated(
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Text(content[index].title),
                    ),
                    subtitle: Text(content[index].createdAt),
                    ),
                  );
                },
                itemCount: content.length,
                separatorBuilder: (context, index) {
                  return Divider(height: 1.0);
                },
              );
            }else if (snapshot.hasError){
              print('error);
              return Container();
            } else {
    
            return Center(
              child: CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation(Colors.blue),
              ),
            );
            }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-04
      • 2020-09-10
      • 2019-08-14
      • 2022-11-27
      • 2020-08-12
      • 2021-05-09
      • 2023-04-01
      • 2020-02-08
      相关资源
      最近更新 更多