'Flutter - Error At 2 Particular Elements Of An Array

I am trying to make a ListView in flutter but i get an error at the element ['제목'], ['내용'] and '['카운트']'

I got this error. The method '[]' can't be unconditionally invoked because the receiver can be 'null'.

This is my code:

 Widget sss(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
      stream: FirebaseFirestore.instance
          .collection('게시판')
          .orderBy('id', descending: true) 
          .snapshots(),
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return new Text('Loading...');
          default:
            return new ListView(
              children: snapshot.data!.docs.map((DocumentSnapshot docs) {
             return new Column(
               children: <Widget>[
                 SizedBox(
                    height: 70,
                    child: new ListTile(
                    title: new Text(docs.data()['제목']!), // <= error
                    subtitle: new Text(docs.data()['내용']!),// <= error 
                         onTap: () {
                            
          onMove(docs.data()['제목']!, docs.data()['내용']!); //<= error
                          }),
                    ),
                    Divider(
                      thickness: 1.0,
                    )
                  ],
                );
              }).toList(),
            );
        }
      },
    );
  }
}

class DataBase {
  FirebaseFirestore firestore = FirebaseFirestore.instance;
  String collection = '게시판';
  void add(String title, String content, String date_time) {
    firestore.collection('카운트')
    .doc('카운트').get().then((DocumentSnapshot ds) {
    
    firestore
    .collection(collection)
    .doc('${ds.data()['카운트']!}')   // <= error

.set(
  {'제목': title, '내용': content, '날짜': date_time, 
   'id':ds.data()['카운트']!});   // <= error
    int id = ds.data()['카운트']! + 1;   // <= error
    cntupdate(id);
  });
}
String data_title = ' ';
String data_content = ' ';

class data {
  void setTitle(String title, String content) {
    if(title!=null) {
      data_title = title;
    }else{data_title = 'null';}
    if(content !=null) {
      data_content = content;
    }else{data_content = 'null';}
  }
}

'''

  DataBase dataBase = new DataBase();

  String title = ' '; 
  String content = ' '; 
  
  child: TextField(
                onChanged: (String text) {
                  if(text != null) {
                    title = text;
                  }else{title = 'null';}
                },

  child: TextField(
                onChanged: (String text) {
                  if(text != null) {
                    content = text;
                  }else{content = 'null';}
                },
 
  child: TextButton(
                  onPressed: () {
                    DataBase dataBase = new DataBase();
                  
                    var now = DateTime.now();
                    dataBase.add(title, content, '$now'); 
                    Navigator.pop(context);
                  },


Solution 1:[1]

Basically, docs.data['...'] can be null but Text required non-null String, so in case docs.data['...'] null it will cause error.

To solve it, add null-check/default-value to it.

Example:

// Text(docs.data['??'])
-> Text(docs.data['??'] ?? 'your default value')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tuan