Flutter:类型 'Future<bool>' 不是类型转换中类型 'bool' 的子类型
Flutter: type 'Future<bool>' is not a subtype of type 'bool' in type cast
我正在尝试根据返回的值显示一个小部件。但是出现以下错误。
type 'Future<bool>' is not a subtype of type 'bool' in type cast
这是导致错误的源代码:
Future<bool> fetchCourses() async {
List courses = [];
final loggedInUser = FirebaseAuth.instance.currentUser;
if (loggedInUser != null) {
final userCollection = await FirebaseFirestore.instance.collection('users').doc(loggedInUser.uid).get();
courses = userCollection.get('coursesEnrolled');
}
if (courses.length == 0) {
return false;
} else {
return true;
}
}
.
.
.
bool hasCourses = fetchCourses() as bool;
.
.
.
hasCourses ? ListAllUserEnrolledCourses() : Container(),
bool hasCourses = await fetchCourses();
你需要等待它完成,但不需要施放它。
fetchCourses()
returns一个Future<bool>
,用FutureBuilder
解析Future
.
FutureBuilder<bool>(
future: fetchCourses(),
builder: (_, snapshot) {
if (snapshot.hasData) {
return snapshot.data ? ListAllUserEnrolledCourses() : Container();
}
return Text('Loading...');
},
),
我正在尝试根据返回的值显示一个小部件。但是出现以下错误。
type 'Future<bool>' is not a subtype of type 'bool' in type cast
这是导致错误的源代码:
Future<bool> fetchCourses() async {
List courses = [];
final loggedInUser = FirebaseAuth.instance.currentUser;
if (loggedInUser != null) {
final userCollection = await FirebaseFirestore.instance.collection('users').doc(loggedInUser.uid).get();
courses = userCollection.get('coursesEnrolled');
}
if (courses.length == 0) {
return false;
} else {
return true;
}
}
.
.
.
bool hasCourses = fetchCourses() as bool;
.
.
.
hasCourses ? ListAllUserEnrolledCourses() : Container(),
bool hasCourses = await fetchCourses();
你需要等待它完成,但不需要施放它。
fetchCourses()
returns一个Future<bool>
,用FutureBuilder
解析Future
.
FutureBuilder<bool>(
future: fetchCourses(),
builder: (_, snapshot) {
if (snapshot.hasData) {
return snapshot.data ? ListAllUserEnrolledCourses() : Container();
}
return Text('Loading...');
},
),