【问题标题】:Flutter and Firebase: Is there a way to run a function on each data snapshot before they are displayed?Flutter 和 Firebase:有没有办法在每个数据快照显示之前对它们运行一个函数?
【发布时间】:2023-04-07 18:24:01
【问题描述】:

我的目标是在将 firebase 中的数据显示给用户之前对其运行解密功能。​​

Here is the database layout

这是显示数据的小部件:

Widget buildPatientCard(BuildContext context, DocumentSnapshot document) {

  final patient = Patient.fromSnapshot(document);

  return new Container(
  child: Card(
    child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.only(top: 8.0, bottom: 4.0),
              child: Row(children: <Widget>[
                Text(patient.name,
                    style: new TextStyle(
                        fontSize: 24.0, fontWeight: FontWeight.bold)),
                Spacer(),
                Text(patient.gender,
                    style: new TextStyle(
                        fontSize: 20.0, fontWeight: FontWeight.bold)),
                Text(", ",
                    style: new TextStyle(
                        fontSize: 20.0, fontWeight: FontWeight.bold)),
                Text(patient.age,
                    style: new TextStyle(
                        fontSize: 20.0, fontWeight: FontWeight.bold))
              ]),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 4.0, bottom: 80.0),
              child: Row(children: <Widget>[
                Text(patient.dob,
                    style: new TextStyle(
                        fontSize: 16.0, fontWeight: FontWeight.bold)),
                Spacer(),
              ]),
            ),
            Row(
              children: <Widget>[
                Text(
                  patient.notes,
                  style: new TextStyle(fontSize: 16.0),
                ),
                Spacer(),
                Icon(Icons.person),

这是患者模型:

Patient.fromSnapshot(DocumentSnapshot snapshot):
patientId = snapshot.documentID,
  name = snapshot['Patient name'],
  gender = snapshot['Gender'],
  age = snapshot['Age'],
  dob = snapshot['Date of Birth'],
  notes = snapshot['Notes'];   

这是我想在每个数据快照显示之前对其运行的解密函数:

  _decrypt() async {
var userKey = await getUserKey();

final decryptedText =
    await FlutterAesEcbPkcs5.decryptString(DATA FROM FIREBASE HERE, userKey);
return decryptedText;  
}  

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    所以我设法通过试验和错误来解决这个问题!这似乎不是最有效的方法(我们稍后会担心),但至少目前它是有效的。

    首先,我创建了一个函数,该函数将使用快照和用户密钥解密患者信息,如下所示:

     Future<String> _decryptName() async {
      var userKey = await getUserKey();
      String decryptText =
          await FlutterAesEcbPkcs5.decryptString(patient.name, userKey);
      return decryptText;
    }
    

    然后我使用 FutureBuilder 等待函数并构建快照,如下所示:

    FutureBuilder(
      future: _decryptName(),
      builder: (context, AsyncSnapshot<String> snapshot) {
       if (snapshot.hasData)
        return Text(
         '${snapshot.data}',
           style: new TextStyle(
            fontSize: 24.0, fontWeight: FontWeight.bold));
       if (snapshot.error)
        return Text(
         'Error getting the data.',
           style: new TextStyle(
            fontSize: 7.0, fontWeight: FontWeight.bold));
             return const CircularProgressIndicator();
       }),
    

    就像我说的 - 这似乎不是最有效的方法,所以如果有人找到更好的方法,请务必告诉我。

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 2021-07-01
      • 2020-03-21
      • 2019-12-31
      • 1970-01-01
      相关资源
      最近更新 更多