好的,这将是一个非常愚蠢的问题,我知道答案就像是1x1,但我似乎无法理解它。
我有一个从在线txt中获取几个链接的函数。这很有效,List<String>
加载了适当的值。
在此之后,我将通过for cycle
迭代项目来启动和处理这些链接。为了不搞乱事情,我使用async / await
,所以在下一次迭代发生之前,应该确保当前链接的完整处理。
这在上周工作,但今天,当我有时间进一步研究我的代码时,似乎有些不可思议。从不知名的地方,当链接应该被处理时,我得到一个例外:
E/flutter (11240): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (11240): type 'String' is not a subtype of type 'List<String>' where
E/flutter (11240): String is from dart:core
E/flutter (11240): List is from dart:core
E/flutter (11240): String is from dart:core
E/flutter (11240):
E/flutter (11240): #0 Initializer.fetchSingleDog (package:osszefogasaszanhuzokert/dogUtilities.dart)
E/flutter (11240): <asynchronous suspension>
E/flutter (11240): #1 Initializer.processLinks (package:osszefogasaszanhuzokert/dogUtilities.dart:46:13)
E/flutter (11240): <asynchronous suspension>
E/flutter (11240): #2 Initializer.main (package:osszefogasaszanhuzokert/dogUtilities.dart:252:11)
E/flutter (11240): <asynchronous suspension>
但是,如果我放置一个断点并使用调试模式,则第一个链接会被正确处理(但是当处理第二个链接时我会得到相同的错误)。并且值也可以正确获取。
以下是我控制整个过程的代码:
main() async {
await fetchLinks();
await processLinks();
}
fetchLinks() async {
myList = await fetchDogList();
}
processLinks() async {
for (int i = 0; i < myList.length; i++) {
await fetchSingleDog(myList[i]);
}
}
fetchSingleDog(var link) async{
final response = await http.get(link);
var document = parse(response.body);
///Parse class elements
name = await processClassElement(document, labelFetchName);
type = await processClassElement(document, labelFetchBreed);
age = await processClassElement(document, labelFetchAge);
color = await processClassElement(document, labelFetchColor);
gender = await processClassElement(document, labelFetchGender);
///Parse container elements
description = await processContainerElement(document, labelFetchDescription);
chip = await processContainerElement(document, labelFetchChip);
neutered = await processContainerElement(document, labelFetchNeutered);
humanBehaviour = await processContainerElement(document, labelFetchHumanBehavior);
childBehaviour = await processContainerElement(document, labelFetchChildBehavior);
petBehaviour = await processContainerElement(document, labelFetchPetBehavior);
sledgeMaleBehaviour = await processContainerElement(document, labelFetchSledgeMaleBehavior);
sledgeFemaleBehaviour = await processContainerElement(document, labelFetchSledgeFemaleBehavior);
otherDogBehaviour = await processContainerElement(document, labelFetchOtherDogBehavior);
otherBehaviour = await processContainerElement(document, labelFetchOtherBehavior);
jailBreak = await processContainerElement(document, labelFetchJailbreak);
otherThings = await processContainerElement(document, labelFetchOtherThings);
foreignAdoption = await processContainerElement(document, labelFetchForeignAdoption);
imageLinks = await processContainerElement(document, labelFetchForeignAdoption);
imageLinks = await fetchDogImageLinks(document);
}
processClassElement(var document, var classElement) async {
result = document.getElementsByClassName(classElement);
var dogInfo = result[0].nodes[1].toString();
return dogInfo.replaceAll(new RegExp(r'"'), '');
}
processContainerElement(var document, var info) async {
var detailsContainer =
document.getElementById('husky_details_container_right');
var information;
switch (info) {
case labelFetchDescription:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[29].nodes[0]
.toString()
.replaceAll(new RegExp('"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchChip:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[3].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchNeutered:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[5].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchHumanBehavior:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[7].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchChildBehavior:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[9].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchPetBehavior:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[11].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchSledgeMaleBehavior:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[15].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchSledgeFemaleBehavior:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[17].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchOtherDogBehavior:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[19].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchOtherBehavior:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[21].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchJailbreak:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[23].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchOtherThings:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[25].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
case labelFetchForeignAdoption:
try {
information = detailsContainer
.nodes[1].nodes[19].nodes[1].nodes[27].nodes[1]
.toString()
.replaceAll(new RegExp(r'"'), '');
} catch (e) {
information = noInformation;
}
break;
default:
break;
}
return information;
}
所以我不确定为什么myLinks[i]
值会导致异常:当我使用调试模式时,它有一个字符串值(应该处理的链接)。使用myLinks[i].toString()
也无济于事......