ListViewm 空值检查运算符用于空值

时间:2021-02-12 19:55:42

标签: flutter dart

我正在尝试从接收到的列表创建一个 ListViewBuilder。我有一个页面,其中传递了一个列表来创建一个 ListViewBuilder。

这是我传递给页面的列表:

flutter: [Device0, botao1, false, de tete 2.4G, tes123]

这就是我尝试创建列表的方式 (DeviceConifgCard):

import 'package:automacaoaplicativo/services/auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DeviceConfigCard extends StatelessWidget {

  final List items;

  DeviceConfigCard(
    this.items, 
  ); //[] para que o icon não seja obrigatorio de colocar
  
  final AuthService _auth = AuthService();
  
  @override
  Widget build(BuildContext context) {
    print(items);
    return GestureDetector(
      onTap: (){
        print(items.length);
      },
      child: ListView.builder(
        padding: EdgeInsets.fromLTRB(15, 5, 15, 0),
        itemCount: items.length,
        itemBuilder: (context, index){
          return Padding(
            padding: const EdgeInsets.fromLTRB(50, 10, 50, 10),
            child: Container(
              width: 100,
              height: 80,
              decoration: BoxDecoration(
                color: Colors.grey[800],
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.circular(20)
              ),
              child: Center(
                child: Text(
                  "ok",
                  style: TextStyle(color: Colors.white, fontSize: 20),
                ),
              ),
            ),
          );
        }
      )
    );
  }
}

这是我得到的错误:


════════ Exception caught by rendering library ═════════════════════════════════
Null check operator used on a null value
The relevant error-causing widget was
ListView
lib/screens_codes/dev_config.dart:27
════════════

这是另一个文件中的第 27 行,此页面调用 de DeviceConifgCard,它从接收到的列表中创建一个简单的列表:

if (snapshot.hasData){
            return ListView.builder(
              padding: EdgeInsets.fromLTRB(15, 5, 15, 0),
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index){
                String nome = snapshot.data.documents[index]['deviceName'];
                String buttonReading = snapshot.data.documents[index]['buttonName'];
                bool resetDevice = snapshot.data.documents[index]['reset_device'];
                String ssid = snapshot.data.documents[index]['ssid'];
                String ssid_pass = snapshot.data.documents[index]['ssid_password'];
                List device_items = [nome, buttonReading, resetDevice, ssid, ssid_pass];
                return Padding(
                  padding: const EdgeInsets.only(top: 10.0),
                  child: DeviceConfigCard(device_items),
                );
              }
            );
          }
    ```

0 个答案:

没有答案