Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

flutter - The argument type 'Future<bool>' can't be assigned to the parameter type 'bool'

I am a beginner in flutter

I try to get the boolean value from the Future< bool> type function

And this my Dart language coding file

I get following error from 104th line

The argument type 'Future< bool > ' can't be assigned to the parameter type 'bool'

Code

isFavorite: _viewForFavorite(photoModel.id),

Full Code

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

Please can anyone find the issue on this code

Thank you for Advanced


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

change your widget like below

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...