未为 class 'Object?' 定义运算符“[]”
The operator '[]' isn't defined for the class 'Object?'
帮我解决下面的问题
import 'package:cloud_firestore/cloud_firestore.dart';
class UserModel{
static const NUMBER = 'number';
static const ID = 'id';
String _number = '';
String _id = '';
String get number => _number;
String get id => _id;
UserModel.fromSnapshot(DocumentSnapshot snapshot){
_number = snapshot.data()![NUMBER];
_id = snapshot.data()![ID];
}
}
有以下错误
lib/models/user_model.dart:16:30: Error: The operator '[]' isn't
defined for the class 'Object?'.
- 'Object' is from 'dart:core'. Try correcting the operator to an existing operator, or defining a '[]' operator.
_number = snapshot.data()[NUMBER];
^ lib/models/user_model.dart:17:26: Error: The operator '[]' isn't defined for the class 'Object?'.
- 'Object' is from 'dart:core'. Try correcting the operator to an existing operator, or defining a '[]' operator.
_id = snapshot.data()[ID];
在最新版本的 cloud firestore 中,DocumentSnapshot 是一个通用类型,因此您需要将类型参数与其一起传递。
示例 -
import 'package:cloud_firestore/cloud_firestore.dart';
class UserModel{
static const NUMBER = 'number';
static const ID = 'id';
String _number = '';
String _id = '';
String get number => _number;
String get id => _id;
UserModel.fromSnapshot(DocumentSnapshot<Map<String,dynamic>> snapshot){
_number = snapshot.data()![NUMBER];
_id = snapshot.data()![ID];
}
}
帮我解决下面的问题
import 'package:cloud_firestore/cloud_firestore.dart';
class UserModel{
static const NUMBER = 'number';
static const ID = 'id';
String _number = '';
String _id = '';
String get number => _number;
String get id => _id;
UserModel.fromSnapshot(DocumentSnapshot snapshot){
_number = snapshot.data()![NUMBER];
_id = snapshot.data()![ID];
}
}
有以下错误
lib/models/user_model.dart:16:30: Error: The operator '[]' isn't defined for the class 'Object?'.
- 'Object' is from 'dart:core'. Try correcting the operator to an existing operator, or defining a '[]' operator. _number = snapshot.data()[NUMBER]; ^ lib/models/user_model.dart:17:26: Error: The operator '[]' isn't defined for the class 'Object?'.
- 'Object' is from 'dart:core'. Try correcting the operator to an existing operator, or defining a '[]' operator. _id = snapshot.data()[ID];
在最新版本的 cloud firestore 中,DocumentSnapshot 是一个通用类型,因此您需要将类型参数与其一起传递。 示例 -
import 'package:cloud_firestore/cloud_firestore.dart';
class UserModel{
static const NUMBER = 'number';
static const ID = 'id';
String _number = '';
String _id = '';
String get number => _number;
String get id => _id;
UserModel.fromSnapshot(DocumentSnapshot<Map<String,dynamic>> snapshot){
_number = snapshot.data()![NUMBER];
_id = snapshot.data()![ID];
}
}