Muhammad Arafat
Muhammad Arafat

Reputation: 83

Flutter null safety problem is : The operator '[]' isn't defined for the type 'Object'

The problem is null safety and migration how to solve The operator '[]' isn't defined for the type 'Object'

```
import 'package:cloud_firestore/cloud_firestore.dart';

 class UserModel {
  static const NUMBER = 'number';
  static const ID = 'id';

 late String _number;
 late String _id;

//getter

 String get number => _number;
 String get id => _id;

 UserModel.fromSnapshot(DocumentSnapshot snapshot) {
  _number = snapshot.data()![NUMBER];
  _id = snapshot.data()![ID];
 }
}
```

Upvotes: 0

Views: 196

Answers (1)

Ruchit
Ruchit

Reputation: 2700

The solution is just to define the CollectionReference more explicitly like so:

 UserModel.fromSnapshot(DocumentSnapshot<Map<String, dynamic>> snapshot) {
  _number = snapshot.data()![NUMBER];
  _id = snapshot.data()![ID];
 }
}

you can read about this here

Upvotes: 2

Related Questions