参数类型'Future<bool>'不能分配给参数类型"bool"



我是flutter 的初学者

我尝试从Future<bool>类型函数

这是我的Dart语言编码文件

我从104行得到以下错误

参数类型'Future<bool>'无法分配给参数类型"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(),
),
);
}

请任何人都能在这个代码上找到问题吗

感谢您的高级

像下面的一样更改小部件

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());
}
},
)
)

当您进入等待时,您已经将这些函数声明为Future

_viewForFavorite(int photoID) async {实际上这与您的退货声明Future<bool> _viewForFavorite(int photoID) async {相同

因此,在调用这些函数时,您要么需要使用FutureBuilder,要么使用wait

您的_viewForFavorite方法是异步的,您在构建方法中调用它时不使用前缀关键字await

isFavorite: await _viewForFavorite(photoModel.id),

因此,您得到的是Future<bool>而不是Bool

您将遇到另一个问题,因为您不能在构建方法中直接调用异步方法。但你可以使用FutureBuilder来解决这个问题。

最新更新