【问题标题】:snapshot is null flutter快照为空颤动
【发布时间】:2021-06-21 00:20:39
【问题描述】:

我尝试使用 FutureBuilder 在 Flutter 的 ListView 中显示 JSON 的结果。当我检查这样的数据时我得到了这个结果

但是当我检查这样的数据时,inspect(snapshot.data['name]);我没有收到任何结果。

我想从 api 获取数据并显示在 listview 中

有什么帮助吗?

我的代码:

@override
  void initState() {
    getproduct(widget.idproduct);
    super.initState();
  }

  Future<dynamic> getproduct(int id) async {
    var response = await Network().getData('/publication/show/$id');
   
    var data = json.decode(response.body)['publication'];
    return data;
  }


 @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
     
      appBar: AppBar(
        brightness: Brightness.light,
        backgroundColor: Colors.transparent,
        elevation: 0,
        leading: GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: Icon(
            Icons.arrow_back,
            color: Colors.grey[800],
          ),
        ),
      ),
      body: Container(
        padding: EdgeInsets.only(left: 16, top: 1, right: 16),
        child: Container(
          child: FutureBuilder(
              future: getproduct(widget.idproduct),
              builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                inspect(snapshot.data);
                return ListView.builder(
                  itemBuilder: (BuildContext context, index) {
                    String price = snapshot.data[index]['price'];
                    String size = snapshot.data[index]['size'];
                    int id = int.parse('${snapshot.data[index]['user_id']}');
                  
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        SizedBox(
                          height: 10.0,
                        ),
                        Text(
                          "$size",
                          style: TextStyle(
                            fontSize: 22.0,
                            color: Colors.grey,
                          ),
                        ),

【问题讨论】:

    标签: api flutter dart


    【解决方案1】:

    由于您的 getProduct 是一个异步方法,因此有一段时间(短或长)值为空。在您的 futurebuilder 中,尝试添加一个 if/else 检查 snapshot.hasData,如下所示:

    FutureBuilder(
              future: getproduct(widget.idproduct),
              builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                inspect(snapshot.data);
                if(snapshot.hasData) {
                  return ListView.builder(...)
                }
                // display something when there is no data in snapshot
              }
            )
    

    【讨论】:

    • 当我检查时 (snapshot.data['name']);我没有收到任何数据。但检查(快照。数据)工作正常。你能帮帮我吗?
    • 您需要向我们提供更多信息,例如来自控制台的错误。
    • type 'String' is not a subtype of type 'int' of 'index' 相关的导致错误的小部件是 FutureBuilder
    • 你能提供你的json吗
    【解决方案2】:

    你的 json 有可能返回一个数组吗?尝试打印snapshot.data[0]['name]

    【讨论】:

    • 打印 (snapshot.data[0]['name]); ==> 它会正确返回我们的名称。我该如何解决?
    • 你不能检查snapshot.data['name]的原因是因为snapshot.data是一个数组,所以它首先需要一个索引。因此错误“类型'String'不是'index'类型'int'的子类型相关的导致错误的小部件是FutureBuilder
    • 我试图解决它,但我做不到。
    猜你喜欢
    • 2021-08-23
    • 2021-10-09
    • 2020-04-12
    • 2022-01-14
    • 2020-03-19
    • 2021-08-08
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多