方法 '[]' 不能无条件调用,因为接收者可以是 'null' [英] The method '[]' can't be unconditionally invoked because the receiver can be 'null'

查看:300
本文介绍了方法 '[]' 不能无条件调用,因为接收者可以是 '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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆