Flutter 参数类型';未来<;布尔>';can';不能分配给参数类型';布尔';
我是颤振的初学者 我尝试从FutureFlutter 参数类型';未来<;布尔>';can';不能分配给参数类型';布尔';,flutter,dart,future,Flutter,Dart,Future,我是颤振的初学者 我尝试从Future类型函数中获取布尔值 这是我的Dart语言编码文件 我从第104行得到以下错误 无法将参数类型“Future”分配给参数类型“bool” 代码 isFavorite:_viewForFavorite(photoModel.id) 完整代码 import '../screens/detailView.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'p
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(),
),
);
}
import'../screens/detailView.dart';
导入“package:cached_network_image/cached_network_image.dart”;
导入“package:shared_preferences/shared_preferences.dart”;
导入“package:favorite_button/favorite_button.dart”;
导入“../models/photoSearch.dart”;
进口“包装:颤振/材料.省道”;
小部件gridPhotosList(构建上下文,列表照片){
_viewForFavorite(int photoID)异步{
final prefs=wait SharedPreferences.getInstance();
最终myStringList=prefs.getStringList('fav_list')??[];
final boolValue=myStringList.contains(photoID.toString());
if(布尔值==true){
返回true;
}否则{
返回false;
}
}
_addForFavorite(int photoID)异步{
final prefs=wait SharedPreferences.getInstance();
最终myStringList=prefs.getStringList('fav_list')??[];
添加(photoID.toString());
prefs.setStringList(“fav_列表”,myStringList);
}
_removeForFavorite(int-photoID)异步{
final prefs=wait SharedPreferences.getInstance();
最终myStringList=prefs.getStringList('fav_list')??[];
myStringList.remove(photoID.toString());
prefs.setStringList(“fav_列表”,myStringList);
}
var size=MediaQuery.of(context).size;
最终双项目高度=(size.height-kToolbarHeight)/2;
最终双项目宽度=(size.width/2);
返回容器(
填充:边缘组。对称(水平:16),
子项:GridView.count(
交叉轴计数:2,
主轴间距:5.0,
交叉轴间距:5.0,
填充:常数边集全部(5.0),
childAspectRatio:(项目宽度/项目高度),
物理:ClampingScrollPhysics(),
收缩膜:对,
孩子:listPhotos.map((照片照片模型){
返回网格(
页脚:GridTileBar(
字幕:文字(摄影模型摄影师),
背景颜色:Colors.black38,
),
子:堆栈(
儿童:[
手势检测器(
onTap:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>DetailView(
imgPath:photoModel.src.grait,
原件:photoModel.src.original,
imgID:photoModel.id,
摄影师:摄影模特,
),
),
);
},
孩子:ClipRRect(
边界半径:边界半径。圆形(4),
子:堆栈(
儿童:[
英雄(
标签:photoModel.src.肖像,
子:容器(
宽度:double.infinity,
高度:双无限,
子:CachedNetworkImage(
imageUrl:photoModel.src.肖像,
占位符:(上下文,url)=>中心(
子对象:CircularProgressIndicator(),
),
适合:BoxFit.cover,
),
),
),
],
),
),
),
对齐(
对齐:alignment.topRight,
子:容器(
装饰:盒子装饰(
颜色:颜色,白色,
borderRadius:仅限borderRadius(
左下角:半径。圆形(25),
),
),
宽度:40,
身高:40,
儿童:中心(
孩子:收藏夹按钮(
isFavorite:_viewForFavorite(photoModel.id),
iconSize:15,
值已更改:(\u isFavorite){
如果(我最喜欢){
_addForFavorite(photoModel.id);
}否则{
_removeForFavorite(photoModel.id);
}
},
),
),
),
),
],
),
);
}).toList(),
),
);
}
任何人都可以找到这个代码的问题吗
感谢您提供的高级功能您在输入wait时已将这些功能声明为未来功能
\u viewForFavorite(int-photoID)async{
实际上,这与您的返回语句与Future\u viewForFavorite(int-photoID)async{
因此,在调用这些函数时,您需要使用FutureBuilder或使用Wait来更改小部件,如下所示
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());
}
},
)
)
您的
\u viewForFavorite
方法是异步的,您可以在构建方法中调用它,而不使用前缀关键字wait
isFavorite: await _viewForFavorite(photoModel.id),
因此,您得到了一个未来
,而不是布尔
您将遇到另一个问题,因为您无法在生成方法中直接调用异步方法。但您可以使用FutureBuilder来解决此问题。这是否回答了您的问题?