当我尝试使用flutter ap在Firestore中显示记录时,出现了一个错误,称为“小部件库引起的异常”

问题描述

这是我遇到的错误

╡小工具图书馆的例外情况提示 ╞═════════════════════════════════════════════════ ══════════ 在构建StreamBuilder(dirty,state:时抛出以下NoSuchMethodError: _StreamBuilderBaseState #0ddea): 吸气剂“部门”在null上被调用。 接收方:空尝试致电:部门 相关的引起错误的小部件是: StreamBuilder

这是代码。我要开发一个在线noticebord。这些代码在新的dart更新之前可以正常工作。在我进行更新之后,它会出现上述错误。请给我一种解决方法

approvenotice.dart

  Widget build(BuildContext context) {

 return StreamProvider<List<Notice>>.value(
  value: NoticeService().notices,child: Scaffold(
  appBar: AppBar(
    elevation: 0.0,title: Text('Aprove Notices',style: TextStyle(
     fontFamily: 'Montserrat',fontWeight: FontWeight.bold,color: Colors.white,),backgroundColor: Colors.blue[800],actions: <Widget>[
     IconButton(
       icon: Icon(Icons.search,onpressed: (){}
       ),],body:UnApprovednotices(),

UnapproveNotices.dart

 class UnApprovednotices extends StatefulWidget {
  @override
 _UnApprovednoticesstate createState() => _UnApprovednoticesstate();
 }

 class _UnApprovednoticesstate extends State<UnApprovednotices> {

  @override
  Widget build(BuildContext context) {
  final notices = Provider.of<List<Notice>>(context) ?? [];

    
    return StreamBuilder<List<Notice>>(
  stream: NoticeService().notices,builder: (context,snapshot) {
    if(snapshot.hasData){
      
      return GridView.builder (
        
      itemCount: notices.length,gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),// ignore: missing_return
      itemBuilder: (context,index){
          return SingleNotice(
          notice:notices[index]

        );
      }
     }
      
      
    );
  }else{
    return(Text('No List'));
  }
    }

尽管在Firestore中有记录,但它显示“ No List”并给出上述错误

解决方法

该错误是由在空值上调用department属性引起的,这意味着, 您尝试调用类似user.department之类的方法,但不幸的是user为空。

我认为,您由于未正确处理AsyncSnapshot而引起麻烦。

为了正确实现快照数据,您需要处理AsyncSnapshot拥有的每个状态。

if (snapshot.hasError) {
  // Return Widget to display error
  // snapshot.error will be not nil.
  return ErrorWidget(snapshot.error);
} else {
   if (snapshot.connectionState == ConnectionState.waiting) {
       // snapshot is waiting for data
       // Generally we could use this to show `CircularProgressIndicator`
       return Center(child: CircularProgressIndicator());
   } else if (snapshot.hasData) {
      // Snapshot will contains particular data.
      // get this value by snapshot.data 
      return DataWidget(snapshot.data);
   }
}

注意:未测试代码。希望这有助于一般实施。如果需要对ConnectionState的更多控制,请执行switch语句。

在flutter文档中了解更多信息:StreamBuilder类。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...