我正在开发Flutter应用程序,该应用程序将Firebase用作后端。在我的Cloud Firestore中,我有两个集合:足球(包含一个文档)和即将到来的团队。
足球中文档的字段:
-字符串currentTeam2
-String即将出现的Team1
-String即将出现的Team2
-int team1Score
-int team2Score
void _nextMatch() {
UpcomingMatch upcomingMatch;
Firestore.instance
.collection('football')
.document("cMrD3OoOsWuzJmzlpfsr")
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
upcomingMatch = UpcomingMatch(
documentSnapshot.data["nextTeam1"].toString(),
documentSnapshot.data["nextTeam2"].toString());
print(documentSnapshot.data["nextTeam1"].toString());
print(documentSnapshot.data["nextTeam2"].toString());
}
});
if (upcomingMatch.upcomingTeam1.isNotEmpty && upcomingMatch.upcomingTeam2.isNotEmpty) {
Firestore.instance
.collection("football")
.document("cMrD3OoOsWuzJmzlpfsr")
.updateData({
"currentTeam1": upcomingMatch.upcomingTeam1,
"currentTeam2": upcomingMatch.upcomingTeam2,
});
}
}
但是,我得到以下错误:
I/flutter ( 8687): The following NoSuchMethodError was thrown while handling a gesture:
I/flutter ( 8687): The getter 'upcomingTeam1' was called on null.
I/flutter ( 8687): Receiver: null
I/flutter ( 8687): Tried calling: upcomingTeam1
I/flutter ( 8687):
I/flutter ( 8687): When the exception was thrown, this was the stack:
I/flutter ( 8687): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I/flutter ( 8687): #1 _MainPageState._nextMatch (package:foci_dev/pages/main_page.dart:178:23)
I/flutter ( 8687): #2 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
I/flutter ( 8687): #3 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
I/flutter ( 8687): #4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
I/flutter ( 8687): #5 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
I/flutter ( 8687): #6 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
I/flutter ( 8687): #7 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:256:7)
I/flutter ( 8687): #8 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:158:27)
I/flutter ( 8687): #9 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:224:20)
I/flutter ( 8687): #10 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
I/flutter ( 8687): #11 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
I/flutter ( 8687): #12 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
I/flutter ( 8687): #13 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
I/flutter ( 8687): #17 _invoke1 (dart:ui/hooks.dart:267:10)
I/flutter ( 8687): #18 _dispatchPointerDataPacket (dart:ui/hooks.dart:176:5)
I/flutter ( 8687): (elided 3 frames from dart:async)
I/flutter ( 8687):
I/flutter ( 8687): Handler: "onTap"
I/flutter ( 8687): Recognizer:
I/flutter ( 8687): TapGestureRecognizer#acb21
如果我正确地解释了这一点,则意味着未分配即将发生的Match的即将出现的Team1属性。我尝试将函数转换为异步并在查询前添加await关键字,但这无济于事。 感谢您提前提供答案!
答案 0 :(得分:1)
将更新内容包含在then
块中,以便futureMatch具有一个值:
void _nextMatch() {
UpcomingMatch upcomingMatch;
Firestore.instance
.collection('football')
.document("cMrD3OoOsWuzJmzlpfsr")
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
upcomingMatch = UpcomingMatch(
documentSnapshot.data["nextTeam1"].toString(),
documentSnapshot.data["nextTeam2"].toString());
print(documentSnapshot.data["nextTeam1"].toString());
print(documentSnapshot.data["nextTeam2"].toString());
}
if (upcomingMatch.upcomingTeam1.isNotEmpty &&
upcomingMatch.upcomingTeam2.isNotEmpty) {
Firestore.instance
.collection("football")
.document("cMrD3OoOsWuzJmzlpfsr")
.updateData({
"currentTeam1": upcomingMatch.upcomingTeam1,
"currentTeam2": upcomingMatch.upcomingTeam2,
});
}
});
}