参数类型 'Future<bool>' 无法分配给参数类型 'bool'
The argument type 'Future<bool>' can't be assigned to the parameter type 'bool'
我是flutter初学者
我尝试从 Future 类型的函数中获取布尔值
这是我的 Dart 语言编码文件
我从第 104 行得到以下错误
The argument type 'Future< bool > ' can't be assigned to the parameter type 'bool'
代码
isFavorite: _viewForFavorite(photoModel.id),
完整代码
import '../screens/detailView.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:favorite_button/favorite_button.dart';
import '../models/photoSearch.dart';
import 'package:flutter/material.dart';
Widget gridPhotosList(BuildContext context, List<Photo> listPhotos) {
_viewForFavorite(int photoID) async {
final prefs = await SharedPreferences.getInstance();
final myStringList = prefs.getStringList('fav_list') ?? [];
final boolValue = myStringList.contains(photoID.toString());
if (boolValue == true) {
return true;
} else {
return false;
}
}
_addForFavorite(int photoID) async {
final prefs = await SharedPreferences.getInstance();
final myStringList = prefs.getStringList('fav_list') ?? [];
myStringList.add(photoID.toString());
prefs.setStringList('fav_list', myStringList);
}
_removeForFavorite(int photoID) async {
final prefs = await SharedPreferences.getInstance();
final myStringList = prefs.getStringList('fav_list') ?? [];
myStringList.remove(photoID.toString());
prefs.setStringList('fav_list', myStringList);
}
var size = MediaQuery.of(context).size;
final double itemHeight = (size.height - kToolbarHeight) / 2;
final double itemWidth = (size.width / 2);
return Container(
padding: EdgeInsets.symmetric(horizontal: 16),
child: GridView.count(
crossAxisCount: 2,
mainAxisSpacing: 5.0,
crossAxisSpacing: 5.0,
padding: const EdgeInsets.all(5.0),
childAspectRatio: (itemWidth / itemHeight),
physics: ClampingScrollPhysics(),
shrinkWrap: true,
children: listPhotos.map((Photo photoModel) {
return GridTile(
footer: GridTileBar(
subtitle: Text(photoModel.photographer),
backgroundColor: Colors.black38,
),
child: Stack(
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailView(
imgPath: photoModel.src.portrait,
original: photoModel.src.original,
imgID: photoModel.id,
photoGrapher: photoModel.photographer,
),
),
);
},
child: ClipRRect(
borderRadius: BorderRadius.circular(4),
child: Stack(
children: [
Hero(
tag: photoModel.src.portrait,
child: Container(
width: double.infinity,
height: double.infinity,
child: CachedNetworkImage(
imageUrl: photoModel.src.portrait,
placeholder: (context, url) => Center(
child: CircularProgressIndicator(),
),
fit: BoxFit.cover,
),
),
),
],
),
),
),
Align(
alignment: Alignment.topRight,
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(25),
),
),
width: 40,
height: 40,
child: Center(
child: FavoriteButton(
isFavorite: _viewForFavorite(photoModel.id),
iconSize: 15,
valueChanged: (_isFavorite) {
if (_isFavorite) {
_addForFavorite(photoModel.id);
} else {
_removeForFavorite(photoModel.id);
}
},
),
),
),
),
],
),
);
}).toList(),
),
);
}
任何人都可以找到此代码的问题
感谢您的进阶
您在进入 await 时已将这些函数声明为 Future
_viewForFavorite(int photoID) async {
实际上这与您的 return 声明相同 Future<bool> _viewForFavorite(int photoID) async {
所以调用这些函数时要么使用FutureBuilder,要么使用await
像下面这样更改您的小部件
Center(
child:
FutureBuilder(
future: _viewForFavorite(photoModel.id),
builder: (c,s){
if(s.hasData){
var newBool = s.data;
return FavoriteButton(
isFavorite: newBool,
iconSize: 15,
valueChanged: (_isFavorite) {
if (_isFavorite) {
_addForFavorite(photoModel.id);
} else {
_removeForFavorite(photoModel.id);
}
},
) ;
}else {
return Center(child: CircularProgressIndicator());
}
},
)
)
您的 _viewForFavorite
方法是异步的,您在构建方法中调用它时没有使用前缀关键字 await
。
isFavorite: await _viewForFavorite(photoModel.id),
因此你得到了 Future<bool>
而不是 Bool
。
您将遇到另一个问题,因为您不能在构建方法中直接调用异步方法。但是您可以使用 FutureBuilder 来解决这个问题。
我是flutter初学者
我尝试从 Future
这是我的 Dart 语言编码文件
我从第 104 行得到以下错误
The argument type 'Future< bool > ' can't be assigned to the parameter type 'bool'
代码
isFavorite: _viewForFavorite(photoModel.id),
完整代码
import '../screens/detailView.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:favorite_button/favorite_button.dart';
import '../models/photoSearch.dart';
import 'package:flutter/material.dart';
Widget gridPhotosList(BuildContext context, List<Photo> listPhotos) {
_viewForFavorite(int photoID) async {
final prefs = await SharedPreferences.getInstance();
final myStringList = prefs.getStringList('fav_list') ?? [];
final boolValue = myStringList.contains(photoID.toString());
if (boolValue == true) {
return true;
} else {
return false;
}
}
_addForFavorite(int photoID) async {
final prefs = await SharedPreferences.getInstance();
final myStringList = prefs.getStringList('fav_list') ?? [];
myStringList.add(photoID.toString());
prefs.setStringList('fav_list', myStringList);
}
_removeForFavorite(int photoID) async {
final prefs = await SharedPreferences.getInstance();
final myStringList = prefs.getStringList('fav_list') ?? [];
myStringList.remove(photoID.toString());
prefs.setStringList('fav_list', myStringList);
}
var size = MediaQuery.of(context).size;
final double itemHeight = (size.height - kToolbarHeight) / 2;
final double itemWidth = (size.width / 2);
return Container(
padding: EdgeInsets.symmetric(horizontal: 16),
child: GridView.count(
crossAxisCount: 2,
mainAxisSpacing: 5.0,
crossAxisSpacing: 5.0,
padding: const EdgeInsets.all(5.0),
childAspectRatio: (itemWidth / itemHeight),
physics: ClampingScrollPhysics(),
shrinkWrap: true,
children: listPhotos.map((Photo photoModel) {
return GridTile(
footer: GridTileBar(
subtitle: Text(photoModel.photographer),
backgroundColor: Colors.black38,
),
child: Stack(
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailView(
imgPath: photoModel.src.portrait,
original: photoModel.src.original,
imgID: photoModel.id,
photoGrapher: photoModel.photographer,
),
),
);
},
child: ClipRRect(
borderRadius: BorderRadius.circular(4),
child: Stack(
children: [
Hero(
tag: photoModel.src.portrait,
child: Container(
width: double.infinity,
height: double.infinity,
child: CachedNetworkImage(
imageUrl: photoModel.src.portrait,
placeholder: (context, url) => Center(
child: CircularProgressIndicator(),
),
fit: BoxFit.cover,
),
),
),
],
),
),
),
Align(
alignment: Alignment.topRight,
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(25),
),
),
width: 40,
height: 40,
child: Center(
child: FavoriteButton(
isFavorite: _viewForFavorite(photoModel.id),
iconSize: 15,
valueChanged: (_isFavorite) {
if (_isFavorite) {
_addForFavorite(photoModel.id);
} else {
_removeForFavorite(photoModel.id);
}
},
),
),
),
),
],
),
);
}).toList(),
),
);
}
任何人都可以找到此代码的问题
感谢您的进阶
您在进入 await 时已将这些函数声明为 Future
_viewForFavorite(int photoID) async {
实际上这与您的 return 声明相同 Future<bool> _viewForFavorite(int photoID) async {
所以调用这些函数时要么使用FutureBuilder,要么使用await
像下面这样更改您的小部件
Center(
child:
FutureBuilder(
future: _viewForFavorite(photoModel.id),
builder: (c,s){
if(s.hasData){
var newBool = s.data;
return FavoriteButton(
isFavorite: newBool,
iconSize: 15,
valueChanged: (_isFavorite) {
if (_isFavorite) {
_addForFavorite(photoModel.id);
} else {
_removeForFavorite(photoModel.id);
}
},
) ;
}else {
return Center(child: CircularProgressIndicator());
}
},
)
)
您的 _viewForFavorite
方法是异步的,您在构建方法中调用它时没有使用前缀关键字 await
。
isFavorite: await _viewForFavorite(photoModel.id),
因此你得到了 Future<bool>
而不是 Bool
。
您将遇到另一个问题,因为您不能在构建方法中直接调用异步方法。但是您可以使用 FutureBuilder 来解决这个问题。