为什么赋值在 Dart 中的工作方式与在 Python 中不同?
Why does assignment not work the same in Dart as it does in Python?
当我运行这段代码时:
void readCard(db, [int card_id = -1]) {
if (card_id == -1) {
final ResultSet result = db.select('SELECT * FROM cards');
}
else {
final ResultSet result = db.select("""
SELECT * FROM cards
WHERE card_id=(?)
"""); // this doesn't work yet
}
for (final Row card in result) {
print(
"Card {'card_id': ${card['card_id']}, "
"'due': ${card['due']}, "
"'content': ${card['content']}}"
);
}
}
我收到这个错误:
memotext.dart:66:25: Error: Getter not found: 'result'.
for (final Row card in result) {
^^^^^^
因为result
不管是if语句还是else语句运行s都赋值了应该不会出错吧?这与 dart 的赋值方式有关吗?
结果在各自的块中声明,它不存在于外部。这样做:
void readCard(db, [int card_id = -1]) {
ResultSet result;
if (card_id == -1) {
result = db.select('SELECT * FROM cards');
}
else {
result = db.select("""
SELECT * FROM cards
WHERE card_id=(?)
"""); // this doesn't work yet
}
for (final Row card in result) {
print(
"Card {'card_id': ${card['card_id']}, "
"'due': ${card['due']}, "
"'content': ${card['content']}}"
);
}
}
当我运行这段代码时:
void readCard(db, [int card_id = -1]) {
if (card_id == -1) {
final ResultSet result = db.select('SELECT * FROM cards');
}
else {
final ResultSet result = db.select("""
SELECT * FROM cards
WHERE card_id=(?)
"""); // this doesn't work yet
}
for (final Row card in result) {
print(
"Card {'card_id': ${card['card_id']}, "
"'due': ${card['due']}, "
"'content': ${card['content']}}"
);
}
}
我收到这个错误:
memotext.dart:66:25: Error: Getter not found: 'result'.
for (final Row card in result) {
^^^^^^
因为result
不管是if语句还是else语句运行s都赋值了应该不会出错吧?这与 dart 的赋值方式有关吗?
结果在各自的块中声明,它不存在于外部。这样做:
void readCard(db, [int card_id = -1]) {
ResultSet result;
if (card_id == -1) {
result = db.select('SELECT * FROM cards');
}
else {
result = db.select("""
SELECT * FROM cards
WHERE card_id=(?)
"""); // this doesn't work yet
}
for (final Row card in result) {
print(
"Card {'card_id': ${card['card_id']}, "
"'due': ${card['due']}, "
"'content': ${card['content']}}"
);
}
}