0

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);
                  },
6
  • post the full error here Commented May 17, 2022 at 4:02
  • Thnk you! The operator '[]' isn't defined for the type 'Object? Function()'. I got this error. Commented May 17, 2022 at 4:33
  • print docs.data!, see what value you get. Commented May 17, 2022 at 4:51
  • Added code at the bottom of the article. If this is not enough, should I add the whole code? Commented May 17, 2022 at 5:10
  • Can you show in which line the error pointed to? Commented May 17, 2022 at 8:30

1 Answer 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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! But, the problem has't been solved. I got this error. The operator '[]' isn't defined for the type 'Object? Function()'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.