从Firebase而不是iexapis检索和处理所有数据

从Firebase而不是iexapis检索和处理所有数据,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我正在使用从cloud.iexapis导入数据的示例应用程序 现在,我在Firestore数据库中保存了一些类似的数据(就像“stockMap”中一样),我只想将cloud.iexapis中的输入替换为Firebase。所有其他的东西都应该像以前一样(例如用股票卡显示) 如何更改代码以用Firebase替换iexapis输入 Future<Null> refreshStockQuotesForUser(String user) async { Firestore.instance

我正在使用从cloud.iexapis导入数据的示例应用程序

现在,我在Firestore数据库中保存了一些类似的数据(就像“stockMap”中一样),我只想将cloud.iexapis中的输入替换为Firebase。所有其他的东西都应该像以前一样(例如用股票卡显示)

如何更改代码以用Firebase替换iexapis输入

 Future<Null> refreshStockQuotesForUser(String user) async {
 Firestore.instance.runTransaction((Transaction transaction) async {
 QuerySnapshot querySnapshot = await Firestore.instance.collection(user)
    .getDocuments();

// Get all documents (stock quotes).
var list = querySnapshot.documents;

list.forEach((doc) async {
  // Get the symbol.
  String symbol = doc.data['stock']['symbol'];

  // Call API for the symbol
  final response =
  await http.get('https://cloud.iexapis.com/stable/stock/$symbol/quote?token=********');
  
  if (response.statusCode == 200) {
    // Get Map from JSON.
    Map data = json.decode(response.body);

    // We are after the data in 'quote'
    Map quoteData = data['quote'];

    // Get relevant fields into a Stock object.
    Stock stock = Stock.fromJson(data);

    Map stockMap = {
      'symbol': stock.symbol,
      'companyName': stock.companyName,
      'latestPrice': stock.latestPrice,
      'low': stock.low,
      'high': stock.high,
      'week52High': stock.week52High,
      'change': stock.change,
      'changePercent': stock.changePercent,
      'peRatio': stock.peRatio,
      'previousClose': stock.previousClose
    };

    // Update Firebase
    Firestore.instance.runTransaction((Transaction transaction) async {
      CollectionReference reference = Firestore.instance.collection(user);

      await reference.document(doc.documentID).updateData(
          {'stock': stockMap}).whenComplete(() {
        print('Stock updated.');
      });
    });
  }
});
 });
 return null;
 }
Future refreshStockQuotesForUser(字符串用户)异步{
Firestore.instance.runTransaction((事务)异步{
QuerySnapshot QuerySnapshot=await Firestore.instance.collection(用户)
.getDocuments();
//获取所有文档(股票报价)。
var list=querySnapshot.documents;
list.forEach((doc)异步{
//获取符号。
字符串symbol=单据数据['stock']['symbol'];
//为符号调用API
最后答复=
等待http.get('https://cloud.iexapis.com/stable/stock/$symbol/quote?标记=********');
如果(response.statusCode==200){
//从JSON获取映射。
Map data=json.decode(response.body);
//我们正在寻找“报价”中的数据
Map quoteData=数据['quote'];
//将相关字段放入股票对象中。
Stock=Stock.fromJson(数据);
Map stockMap={
“symbol”:stock.symbol,
“companyName”:stock.companyName,
“最新价格”:stock.latestPrice,
“低”:stock.low,
“高”:stock.high,
“week52High”:stock.week52High,
“change”:stock.change,
“changePercent”:stock.changePercent,
“peRatio”:stock.peRatio,
“previousClose”:stock.previousClose
};
//更新Firebase
Firestore.instance.runTransaction((事务)异步{
CollectionReference=Firestore.instance.collection(用户);
等待reference.document(doc.documentID).updateData(
{'stock':stockMap})。完成((){
打印(“库存更新”);
});
});
}
});
});
返回null;
}