我寫了一個谷歌云函式,回傳一個數字。我想在我的flutter應用程式中讀取該數字,但我無法做到。看起來 Flutter http 依賴關系只能讀取 JSON 格式。
我已經嘗試在云函式中格式化回應輸出,但我一直收到錯誤資訊,說沒有找到flask。我使用的是 Python 3.9,根據 這個鏈接,它應該是默認擁有 flask。我也試過將輸出格式化為字典,然后使用json.dumps(x),但這也不起作用。
這個函式應該用來檢索資料:
Future<http.Response> dataHTTP() async {
return http.get(
Uri.parse(
('https://us-east4-persuasive-yeti-325421.cloudfunctions.net/open_seats?college'
college.text.toUpperCase()
'& dept='
department.text.toUpperCase()
'&course=' department.text.toUpperCase()
course.text.toUpperCase()
'§ion='
section.text.toUpperCase())。
),
);
}
我想列印輸出到終端,以檢查事情是否正常。但是下面的代碼片段回傳以下內容。Instance of 'Future<Response>'
TextButton(
onPressed: () async {
print(dataHTTP().toString())。
},
孩子。Text('Enter')。
)
我怎樣才能回傳資料本身而不是物件實體?是在云端函式中格式化輸出,還是在客戶端處理更容易呢?
uj5u.com熱心網友回復:
你得到的是Instance of 'Future<Response>',因為你列印的是Future物件的toString()方法,而不是回應本身。
當你使用async并且你需要該async任務的結果時,你需要為它 "等待"。
如果你想保留方法簽名,你可以在你的onPressed函式中對回應進行等待,就像這樣:
TextButton(
onPressed: () async {
http.Response response = await dataHTTP();
print(response.body)。
},
孩子。Text('Enter')。
)
你可以在這個codelab中獲得更多關于用Dart進行異步編程的資訊
uj5u.com熱心網友回復:我遇到的問題是,http請求只包含一個整數值。為了在我的flutter應用程式上顯示這個值,我所要做的就是:
TextButton(
onPressed: () async {
http.Response response = await dataHTTP();
print(response.body)。
},
孩子。Text('Enter')。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/311849.html
標籤:
