1
import 'package:firebase_database/firebase_database.dart';

class Post {
  static const KEY = "key";
  static const DATE = "date";
  static const TITLE = "title";
  static const BODY = "body";
  final String date;
  String key;
  final String title;
  final String body;

  Post(this.date, this.key, this.title, this.body);

  // String get ket => _key;
  //
  // String get date => _date;
  //
  // String get title => _title;
  //
  // String get body => _body;

  Post.fromSnapshot(DataSnapshot snap)
      : key = snap.key.toString(),
        body = snap.value[BODY],
        date = snap.value[DATE],
        title = snap.value[TITLE];

  toMap() {
    return {BODY: body, TITLE: title, DATE: date};
  }
}

error:

The method '[]' can't be unconditionally invoked because the receiver can be 'null'. (unchecked_use_of_nullable_value at [flutter_firebase] lib\models\post.dart:25)

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Welcome to Stack Overflow! Please take the [tour] and read [ask]. Your questions about code, i.e. debugging questions like these, are served by not only providing the code + error message, but also a textual description on what the code is supposed to do, and what it currently does, see [mcve]. Also showing what you have already tried to solve the problem is appreciated. Please [edit] the question accordingly. – Adriaan Jun 27 '22 at 07:46
  • Does this answer your question? [The method '\[\]' can't be unconditionally invoked because the receiver can be 'null'](https://stackoverflow.com/questions/67575893/the-method-cant-be-unconditionally-invoked-because-the-receiver-can-be-nu) – Adriaan Jun 27 '22 at 07:53

1 Answers1

2

The value in snap.value can be null on line 25. And then, trying to use the index operator [] on it (a possible null value) shows this error (It'd be the same as doing null[]).

Solution 1

Try checking where the data come from if it can be null. If that's the case, i.e. if null is really possible from DataSnapshot, you'd need to specify Post properties with a nullable type as well with the ? after the type like so:

class Post {
  static const KEY = "key";
  static const DATE = "date";
  static const TITLE = "title";
  static const BODY = "body";
  final String? date;
  String key;
  final String? title;
  final String? body;

  Post(this.date, this.key, this.title, this.body);

  ...

And then default the value to something else using the ? unary postfix operator in conjunction with ?? if-null operator if it comes as null like the code snippet below. You'd need also to cast the snap.value, that is a Object? in the new Firestore 9.x, to a nullable Map<String, dynamic>?.

  Post.fromSnapshot(DataSnapshot snap)
      : key = snap.key.toString(),
        body = (snap.value as Map<String, dynamic>?)?[BODY] ?? '',
        date = (snap.value as Map<String, dynamic>?)?[DATE] ?? '',
        title = (snap.value as Map<String, dynamic>?)?[TITLE] ?? '';

Solution 2

Otherwise, if null is not a possibility at all then you can force the value to be non-null with the ! null assertion operator, casting away nullability, like so:

  Post.fromSnapshot(DataSnapshot snap)
      : key = snap.key.toString(),
        body = (snap.value! as Map<String, dynamic>)[BODY],
        date = (snap.value! as Map<String, dynamic>)[DATE],
        title = (snap.value! as Map<String, dynamic>)[TITLE];
lepsch
  • 8,927
  • 5
  • 24
  • 44
  • I've edited my anwser with an amendment to solution 1. Please check – lepsch Jun 27 '22 at 08:05
  • hii @lepsch i tried both of the solution but it give me the error "error: The operator '[]' isn't defined for the type 'Object'. (undefined_operator at [flutter_firebase] lib\models\post.dart:25)" –  Jun 27 '22 at 08:08
  • 1
    Most probably you're gonna need to cast `snap.value` to a `Map` like: `(snap.value! as Map)[BODY]` and so on. Please try it so I can update the answer – lepsch Jun 27 '22 at 08:15