Flutter:使用 Getx 从 firebase 读取数据

Flutter : read data from firebase using Getx

我正在尝试在页面上显示数据 但是这个错误在我看来 有解决办法吗?

控制器

RxInt pprofit = 0.obs ; 

void onInit() async{
      super.onInit();
    // TODO: implement onInit

      CollectionReference fprofits = FirebaseFirestore.instance.collection('admin');
      await fprofits.doc('profits').get().then((value) {
        pprofit = value.data()['profits'];
      }); }

我只想获取这个值

错误

    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.
        pprofit = value.data()['profits'];

也许这可以帮到你

CollectionReference fprofits = FirebaseFirestore.instance;

final collection = await fprofits.collection('admin').doc('profits').get();
final data = collection.data();
print(data);

你可以在地图中转换它

CollectionReference fprofits = FirebaseFirestore.instance;

final collection = await fprofits.collection('admin').doc('profits').get();
Map<String, dynamic> data = collection.data() as Map<String, dynamic>;
print(data);

告诉你是否对你有用