Flutter flatter/Firebase-If/Else语句仅在瞬间返回中心小部件

Flutter flatter/Firebase-If/Else语句仅在瞬间返回中心小部件,flutter,dart,Flutter,Dart,我正在尝试从firebase加载数据。为了处理没有返回数据的事件,我实现了一个if/else语句。当我不希望返回任何数据时,文本小部件(嵌套在中心小部件中)会在瞬间显示(是,因为snapshot.hasData返回true仅当只有数据时,但是false在加载时或出错时,因此您看到文本,因为数据正在加载,所以最好移动并返回文本(“出错”)在快照.hasError下,否则返回一个加载程序 Widget buildProductList( BuildContext上下文,异步快照(快照){ if(s

我正在尝试从firebase加载数据。为了处理没有返回数据的事件,我实现了一个if/else语句。当我不希望返回任何数据时,文本小部件(嵌套在中心小部件中)会在瞬间显示(是,因为
snapshot.hasData
返回
true
仅当只有数据时,但是
false
加载时或出错时,因此您看到文本,因为数据正在加载,所以最好移动并返回
文本(“出错”)
快照.hasError
下,否则返回一个
加载程序

Widget buildProductList(
BuildContext上下文,异步快照(快照){
if(snapshot.hasData){
返回GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
收缩膜:对,
itemCount:snapshot.data.docs.length,
itemBuilder:(上下文,索引){
返回容器(
身高:25.0,
宽度:25.0,
子级:映像(映像:FirebaseImage(snapshot.data.docs[index]['imgUrl'],maxSizeBytes:15*1024*1024));
});
}else if(snapshot.hasrerror){
//有点不对劲
返回中心(
孩子:文本(“出了问题”),
);
}否则{
//仍在加载
返回中心(
子对象:CircularProgressIndicator(),
);
}
}

通过将我的代码重写如下,成功解决了问题:

class ProductListPage extends StatelessWidget {
  String event;

  ProductListPage({this.event});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.deepPurpleAccent,
        title: Text(
          " Cards",
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('products')
            .where('event', isEqualTo: event)
            .snapshots(),
        builder: buildProductList,
      ),
    );
  }
}
Widget buildProductList(
    BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
  if (snapshot.hasData) {
    if(snapshot.data.docs.length == 0) {
      return Center(
        child: Text('Something went wrong'),
      );
    } else{
    return GridView.builder(
        gridDelegate:
        SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
        shrinkWrap: true,
        itemCount: snapshot.data.docs.length,
        itemBuilder: (context, index) {
          return Container(
              height: 25.0,
              width: 25.0,
              child:  Image(image: FirebaseImage(snapshot.data.docs[index]['imgUrl'], maxSizeBytes: 15 * 1024 * 1024)));
        });
  }}else{
    // Still loading
    return Center(
      child: CircularProgressIndicator(),
    );
  }
}
class ProductListPage扩展了无状态小部件{
字符串事件;
ProductListPage({this.event});
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
背景颜色:颜色。深紫色,
标题:正文(
“卡片”,
样式:TextStyle(颜色:Colors.white),
),
),
正文:StreamBuilder(
流:FirebaseFirestore.instance
.集合(“产品”)
.where('事件',isEqualTo:event)
.snapshots(),
生成器:buildProductList,
),
);
}
}
小部件构建产品列表(
BuildContext上下文,异步快照(快照){
if(snapshot.hasData){
if(snapshot.data.docs.length==0){
返回中心(
孩子:文本(“出了问题”),
);
}否则{
返回GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
收缩膜:对,
itemCount:snapshot.data.docs.length,
itemBuilder:(上下文,索引){
返回容器(
身高:25.0,
宽度:25.0,
子级:映像(映像:FirebaseImage(snapshot.data.docs[index]['imgUrl'],maxSizeBytes:15*1024*1024));
});
}}否则{
//仍在加载
返回中心(
子对象:CircularProgressIndicator(),
);
}
}

感谢这个ikerfah,我仍然有中心小部件消失的问题。我尝试了if(snapshot.data==null)作为条件,因为我认为没有错误,只是没有返回数据,这并没有解决问题。还有其他想法吗?你能更新你现在使用的代码吗?谢谢你的帮助,我设法找到了解决方案。
Widget buildProductList(
    BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
  if (snapshot.hasData) {
    return GridView.builder(
        gridDelegate:
        SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
        shrinkWrap: true,
        itemCount: snapshot.data.docs.length,
        itemBuilder: (context, index) {
          return Container(
            height: 25.0,
            width: 25.0,
            child:  Image(image: FirebaseImage(snapshot.data.docs[index]['imgUrl'], maxSizeBytes: 15 * 1024 * 1024)));
        });
  } else if(snapshot.hasError) {
    // Something is wrong
    return Center(
      child: Text('Something went wrong'),
    );
  }else{
    // Still loading
    return Center(
       child: CircularProgressIndicator(),
    );
  }
}
class ProductListPage extends StatelessWidget {
  String event;

  ProductListPage({this.event});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.deepPurpleAccent,
        title: Text(
          " Cards",
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('products')
            .where('event', isEqualTo: event)
            .snapshots(),
        builder: buildProductList,
      ),
    );
  }
}
Widget buildProductList(
    BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
  if (snapshot.hasData) {
    if(snapshot.data.docs.length == 0) {
      return Center(
        child: Text('Something went wrong'),
      );
    } else{
    return GridView.builder(
        gridDelegate:
        SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
        shrinkWrap: true,
        itemCount: snapshot.data.docs.length,
        itemBuilder: (context, index) {
          return Container(
              height: 25.0,
              width: 25.0,
              child:  Image(image: FirebaseImage(snapshot.data.docs[index]['imgUrl'], maxSizeBytes: 15 * 1024 * 1024)));
        });
  }}else{
    // Still loading
    return Center(
      child: CircularProgressIndicator(),
    );
  }
}