我正在尝试从Firebase获取一组加密的数据文档,并将其显示在flutter的列表视图中。
我使用流生成器获取数据,并开始在列表视图中显示它。但是我无法对每个数据项执行解密操作,因为它是异步操作。做这个的最好方式是什么?
<form name="login" action="" method="post" id="frmRegister" >
<input type="text" name="FName" id="FName" placeholder="First Name"/>
<input type="text" name="LName" id="LName" placeholder="Last Name"/>
<input type="text" name="userName" id="userName" placeholder="User
Name"/>
<input type="password" name="password" id="password"
placeholder="Password"/>
<button type="submit" name="btnRegister" id="btnRegister"
value="Register" onclick="submitclick()" >Register</button>
</div>
</form>
答案 0 :(得分:0)
根据您的情况,可以将 StreamController 与帮助器类一起使用以保存信息。
以下仅是示例,但可以根据自己的需要进行调整。
// Helper classes
// adapt it to your own business case
class Notes {
String title;
String description;
Notes({this.title, this.description});
}
class NotesFromDb {
String connectionState;
bool hasError;
String error;
List<Notes> notes;
NotesFromDb({this.connectionState, this.hasError, this.error, this.notes});
}
// The Streambuilder
StreamBuilder<NotesFromDb>(
stream: sController.stream,
builder: (BuildContext context, AsyncSnapshot<NotesFromDb> snapshot) {
// Here you can check for errors
if (snapshot.data.hasError == true) {
return Container(
color: Colors.red,
child: Text(snapshot.data.error),
);
}
// Here you can check for your connection state
if (snapshot.data.connectionState == 'Loading') {
return Container(
color: Colors.yellow,
child: CircularProgressIndicator(),
);
}
// Here you can show your data
var info = snapshot.data.notes.map((doc) {
return Text(doc.title);
});
return Center(
child: Container(
color: Colors.deepOrange,
child: Column(
children: info.toList(),
),
));
})
// Here is how you can handle the decrypt data
// using a FloatingActionButton for loading the data (for example)
FloatingActionButton(
onPressed: () async { // you would need to add the async
List<Notes> theNotes; //just to hold the information
// Use this to allow to show the CircularProgressIndicator
sController.sink.add(NotesFromDb(connectionState: 'Loading'));
var snapshots = Firestore.instance.collection('notes').snapshots();
snapshots.listen((QuerySnapshot data) {
theNotes = data.documents.map((DocumentSnapshot doc) {
// Build your data
return Notes(
title: doc.data['title'],
description: doc.data['description']);
}).toList();
}, onError: (err, stack) {
// If an error happend then send the error to the stream
sController.sink
.add(NotesFromDb(hasError: true, error: err.error));
});
// Here you can to decrypt the documents with your function
var decryptDocuments = await Future.delayed(Duration(seconds: 2)); //Whatever future function
// Once you have the decrypt documents, you would need to send that to the stream.
// set the connectionState to Done, so the spinner is not showed anymore.
sController.sink.add(NotesFromDb(
hasError: false, connectionState: 'Done', notes: decryptDocuments));
},
child: Icon(Icons.arrow_forward),
)
仅以示例为例进行说明。
希望有帮助。