[英]Flutter/Dart - Suggestions for how to Troubleshoot a Future?
雖然它以前可以工作,但由於某種原因,我的代碼現在已經停止工作。 盡管它會獲取所需的 json 數據,但構建不會呈現。 我的應用頁面上應該顯示頁面視圖的錯誤是;
type String is not a subtype of 'Map<String,dynamic>'
但是經過一些調整,現在錯誤是;
invalid arguments(s)
我想我可能已經把它縮小到了未來;
FutureBuilder<List<SpeakContent>> futureStage() {
return new FutureBuilder<List<SpeakContent>>(
future: downloadJSON(),
builder: (context, snapshot) {
if (snapshot.hasData) {
print("Snapshot has data.");
List<SpeakContent> speakcrafts = snapshot.data;
return new CustomPageView(speakcrafts);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return new CircularProgressIndicator();
},
);
}
Future<List<SpeakContent>> downloadJSON() async {
final jsonEndpoint =
"http://example.com/getContent.php?";
final response = await get(jsonEndpoint);
if (response.statusCode == 200) {
List speakcrafts = json.decode(response.body);
debugPrint(speakcrafts.toString());
return speakcrafts
.map((speakcraft) => new SpeakContent.fromJson(speakcraft))
.toList();
} else
throw Exception('We were not able to successfully download the json data.');
}
雖然它沒有拋出錯誤,但我注意到它沒有在“if (snapshot.hasData)”行之后打印我的測試語句。
我不應該看到“快照有數據”嗎? 出現在我的 Android Studio 控制台中?
根據您提供的,這
type String is not a subtype of 'Map<String,dynamic>'
必須是這樣的:
return Text('${snapshot.error}');
這意味着您的downloadJSON()
引發了異常。 在這種情況下, print('Snapshot has data.');
從不執行,我上面引用的下一個案例將被執行。
請在downloadJSON()
的主體中放置一個斷點,逐行運行它,看看在哪里拋出了什么。
另外,您在這里犯了一個無關緊要但很大的錯誤。 不要像這樣調用downloadJSON()
。 這個 function 在每次重新渲染時執行,可以多次。 每次重繪小部件時,您都會啟動 JSON 下載。 這可能會增加您的后端賬單......我在這次談話中更詳細地解釋它: https://youtu.be/vPHxckiSmKY?t=4771
在按照 Gazihan Alankus 和 scrimau 的建議執行故障排除提示后,我發現罪魁禍首是 MYSQL 數據庫中的單個 null 條目。 可怕......必須找出如何在未來防止這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.