【问题标题】:listview.separated flutter showing blank page even it has datalistview.separated 颤动显示空白页,即使它有数据
【发布时间】:2020-09-17 02:37:01
【问题描述】:

嗨,我是 Flutter 和 dart 的新手,我有这个问题,即页面没有显示数据,即使 listView.seperator 中有数据,当我检查 dubugger 时,我会在 snapshot.data[index] 中获取数据.finishedProductId 或finishedProductName 或categoryShortName,但调试器完成它显示空白页,甚至删除除Text 之外的所有内容,仍然显示空白页我正在提供代码,请帮助我。我提前给我的代码如下。为了安全起见,我用 start 标记了我的网站链接。对此感到抱歉。

import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;

class DetailsPage extends StatefulWidget {
  final String itemCode;

  DetailsPage({Key key, this.itemCode}) : super(key: key);

  @override
  _DetailsPageState createState() => _DetailsPageState();
}

class _DetailsPageState extends State<DetailsPage> {
  // ignore: missing_return
  Future<List<ItemDetails>> _getItemDetails() async {
    final response = await http.get(
        'https://www.****.com/appfiles/getprodcontent.php?getContent=' +
            this.widget.itemCode );

    var jsonData = jsonDecode(response.body);

    print(jsonData);

    List<ItemDetails> itemDet = [];

    for (var it in jsonData) {
      ItemDetails iDet = ItemDetails(
          it['imgPath'],
          it['finishedProductId'],
          it["finishedProductName"],
          it["categoryShortName"],
          );

      itemDet.add(iDet);
      
    }
    print(itemDet.length);
    return itemDet;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueGrey[200],
      appBar: AppBar(
        title: Text("Product Details of " + this.widget.itemCode),
      ),
      body: Container(
          child: FutureBuilder(
              
              initialData: [],
              future: _getItemDetails(),
              // ignore: missing_return
              builder: (BuildContext context, AsyncSnapshot snapshot) {
              
              //if(snapshot.connectionState == ConnectionState.done){  
              
                //if (snapshot.data == null) {
                 // return Container(
                   //   child: Center(
                   // child: Text('Loading...'),
                 /// ));
               // } else {

                  return Column(

                    children: <Widget> [
                      SizedBox(
                        height:200,
                      
                     child: ListView.seperated(
                        
                          padding: const EdgeInsets.all(8),
                          itemCount: snapshot.data.length,
                          // ignore: missing_return
                          itemBuilder: (BuildContext context, int index) {
                            
                            Container (
                              child: 
                                Center(child: Text("finished product id") ,),                  
                            );
                    
                            Container(
                              height: 50,
                              color: Colors.amber[200],
                              child: Center(
                                child: CircleAvatar(
                                  radius: 50.0,
                                  backgroundImage: NetworkImage(
                                      "https://www.shardaudyogmandir.com/assets/thumbs/javaschatni.png")
                                      != null ?NetworkImage("https://www.****.com/assets/thumbs/***.png") : Container()                             ,
                                ),
                              ),
                            );
                           
                            
                            Container(
                              height: 50,
                              color: Colors.amber[200],
                              child: Center(
                                child: Text(snapshot.data[index].finishedProductId != null ? snapshot.data[index].finishedProductId: "",
                                    style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 20,
                                        fontWeight: FontWeight.bold)),
                              ),
                            );

                            Container(
                              height: 50,
                              color: Colors.amber[200],
                              child: Center(
                                child: Text(snapshot.data[index].finishedProductName != null ? snapshot.data[index].finishedProductName: "",
                                                style: TextStyle(color: Colors.black)),
                                            
                              ),
                            );

                            Container(
                              height: 50,
                              color: Colors.amber[200],
                              child: Center(
                                child: Text(snapshot.data[index].categoryShortName != null ? snapshot.data[index].categoryShortName : "",
                                                style: TextStyle(color: Colors.black)),
                                            
                              ),
                            );

                            Container(
                              height: 50,
                              color: Colors.amber[200],
                              child: Center(
                                child:  RaisedButton(
                                              color: Colors.green[700],
                                              onPressed: () {
                                                //print("press view details");
                                              },
                                              child: const Text('Add To Cart',
                                                  style: TextStyle(
                                                      fontSize: 20,
                                                      color: Colors.white)),
                                            ),
                                            
                              ),
                            );
                          },
                          //separatorBuilder: (BuildContext context, int index) =>
                          //const Divider(),
                        ),
                      )
                    ],
                  );
                 
                //}
              //}
              }
              )
              ),
    );
  }
}

class ItemDetails {
  final String finishedProductId;
  final String finishedProductName;
  final String categoryShortName;
  //final String rawMaterialName;
  final String imgPath;

  ItemDetails(this.imgPath, this.finishedProductId, this.finishedProductName,
      this.categoryShortName);
}

【问题讨论】:

  • 尝试将您的ListView.separated 包裹在Container 中,并给它正确的heightwidth
  • 我试过你的选择,但没有运气......仍然给出相同的空白页......

标签: flutter listview dart


【解决方案1】:

您需要像这样在 Item Builder 方法中添加 return :

                itemBuilder: (BuildContext context, int index) {
                  
                  return Column(
                    children: [
                      Container(
                    height: 50,
                    color: Colors.amber[200],
                    child: Center(
                      child: CircleAvatar(
                        radius: 50.0,
                        backgroundImage: NetworkImage(
                            "https://www.******.com/assets/thumbs/****.png")
                            != null ?NetworkImage("https://www.shardaudyogmandir.com/assets/thumbs/javaschatni.png") : Container()                             ,
                      ),
                    ),
                  ),
                 
                  Container (
                    child: 
                      Center(child: Text("finished product id") ,),                  
                  ),
                  Container(
                    height: 50,
                    color: Colors.amber[200],
                    child: Center(
                      child: Text(snapshot.data[index].finishedProductId != null ? snapshot.data[index].finishedProductId: "",
                          style: TextStyle(
                              color: Colors.black,
                              fontSize: 20,
                              fontWeight: FontWeight.bold)),
                    ),
                  ),

                  Container(
                    height: 50,
                    color: Colors.amber[200],
                    child: Center(
                      child: Text(snapshot.data[index].finishedProductName != null ? snapshot.data[index].finishedProductName: "",
                                      style: TextStyle(color: Colors.black)),
                                  
                    ),
                  ),

                  Container(
                    height: 50,
                    color: Colors.amber[200],
                    child: Center(
                      child: Text(snapshot.data[index].categoryShortName != null ? snapshot.data[index].categoryShortName : "",
                                      style: TextStyle(color: Colors.black)),
                                  
                    ),
                  ),

                  Container(
                    height: 50,
                    color: Colors.amber[200],
                    child: Center(
                      child:  RaisedButton(
                                    color: Colors.green[700],
                                    onPressed: () {
                                      //print("press view details"),
                                    },
                                    child: const Text('Add To Cart',
                                        style: TextStyle(
                                            fontSize: 20,
                                            color: Colors.white)),
                                  ),
                                  
                    ),
                  ),
                    ],
                  );
                },

并将其包裹在列内。我不知道 UI 会是什么样子,但这就是您的代码的问题。欲了解更多信息,请前往Flutter.Dev

【讨论】:

  • 我也尝试了@hamza 和你的 ans,但它给出了错误 Build function must never return null。请帮我解决这个问题
  • 也许你一直忽略丢失的回报,在FutureBuilder 小部件中你不需要在if 之后放置其他内容,只需放置if,然后在外面放置你的回报ListView.separated
  • 我已经编辑了主帖,因为我的回答是阻止。请查看更改。我添加了列,评论了如果条件我仍然得到相同的空白页。再次提前感谢
猜你喜欢
  • 2021-02-04
  • 2021-09-03
  • 2013-11-17
  • 2015-06-09
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 2014-07-19
  • 2015-06-06
相关资源
最近更新 更多