方法 '[]' 不能无条件调用,因为接收者可以是 'null'

编程入门 行业动态 更新时间:2024-10-15 10:17:35
本文介绍了方法 '[]' 不能无条件调用,因为接收者可以是 'null'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是 Flutter 的新手.我正在尝试开发一个应用程序.

I'm new to Flutter. I am trying to develop an application.

我想显示 Firebase 数据库中的人员列表.但是,我收到以下错误.

I want to show the staff list in the Firebase database. However, I am getting the following error.

错误:

方法'[]'不能无条件调用,因为接收者可以为空".尝试使呼叫有条件(使用 '?.')或添加对目标的空检查 ('!').

The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').

科德拉姆:

`import 'package:calendar/page/mainPage.dart'; import 'package:flutter/material.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; class Staff extends StatefulWidget { @override _StaffState createState() => _StaffState(); } class _StaffState extends State<Staff> { final _firestore = FirebaseFirestore.instance; @override Widget build(BuildContext context) { // ignore: unused_local_variable CollectionReference staffRef = _firestore.collection('staff'); return Scaffold( appBar: AppBar( title: Text("Personel Listesi"), backgroundColor: Colors.redAccent[400], actions: <Widget>[ IconButton( icon: Icon(Icons.home), onPressed: () { Navigator.pushAndRemoveUntil( context, MaterialPageRoute(builder: (_) => MainPage()), (route) => true); }, ), ], ), body: Container( child: Padding( padding: const EdgeInsets.all(8.0), child: Center( child: Column( children: [ StreamBuilder<QuerySnapshot>( stream: staffRef.snapshots(), builder: (BuildContext context, AsyncSnapshot asyncSnapshot) { if (asyncSnapshot.hasError) { return Center( child: Text( "Bir hata oluştu, lütfen tekrar deneyiniz.")); } else { if (asyncSnapshot.hasData) { List<DocumentSnapshot> listStaff = asyncSnapshot.data.docs; return Flexible( child: ListView.builder( itemBuilder: (context, index) { return Card( elevation: 20, color: Colors.greenAccent[200], child: ListTile( trailing: IconButton( icon: Icon(Icons.delete), onPressed: () async { await listStaff[index] .reference .delete(); }, ), title: Text( '${listStaff[index].data['nameSurname']}', style: TextStyle(fontSize: 20), ), subtitle: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Text( '${listStaff[index].data['tip']}', style: TextStyle(fontSize: 14), ), ], ), Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Text( '${listStaff[index].data['mail']}', style: TextStyle(fontSize: 14), ), ], ), Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Text( '${listStaff[index].data['phone']}', style: TextStyle(fontSize: 14), ), ], ), ], ), ), ); }, itemCount: listStaff.length), ); } else { return Center( child: CircularProgressIndicator(), ); } } }, ), ], ), ), ), ), ); } } `

推荐答案

在新的flutter更新中,我们不需要添加.data()

In the new flutter update, we don't need to add .data()

我的代码如下

title: Text( **'${listStaff[index].data['nameSurname']}',** style: TextStyle(fontSize: 20), ),

像这样改变它修复了错误.

title: Text( **'${listPersonel[index]['nameSurname']}'**, style: TextStyle(fontSize: 20), ),

更多推荐

方法 '[]' 不能无条件调用,因为接收者可以是 'null'

本文发布于:2023-11-25 02:19:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收者   方法   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!