【问题标题】:flutter - Future<bool> convert to bool颤振 - Future<bool> 转换为 bool
【发布时间】:2020-07-04 20:48:14
【问题描述】:

我有一个未来的 bool 方法,我想使用这个方法 IconButton 颜色。如果我使用以下代码,我会在我的设备屏幕上看到一条错误消息type'Future' is not a subtype of type 'bool' in type of cast.

Future<bool> ifExistInFavoriteList(String url) async {

bool ifExists = false;

SharedPreferences 首选项 = 等待 SharedPreferences.getInstance(); 列出我的 = (prefs.getStringList('myFavoriteList') ?? List());

my.contains(url) ? ifExists = true : ifExists = false;

如果存在则返回;

}

  bool _isLiked() {
bool a = false;
a = ifExistInFavoriteList(widget.imageUrl) as bool;

return a;

} }

Expanded(
                      child: IconButton(
                        color:
                            _isLiked() ? Colors.deepPurple : Colors.green, 
                        icon: Icon(Icons.category),
                        onPressed: () {
                          //TO-DO
                        },
                      ),
                    )

【问题讨论】:

    标签: flutter dart boolean


    【解决方案1】:

    您不能简单地将 Future 类型转换为 bool。您需要使用 await 或 then 语法从该未来获取 bool 值。但我建议您使用FutureBuilder,这将是最好的解决方案。

    FutureBuilder(future: ifExistInFavoriteList(widget.imageUrl),
                  builder:(context, snapshot) {
        Color iconColor = Colors.green;
        if (snapshot.hasData && snapshot.data) {
                iconColor = Colors.purple;
        }
        return IconButton(color: iconColor,
                          icon: Icon(Icons.category),
                          onPressed: () {
                             //TO-DO
                          },
               );
         },
    ),
    

    【讨论】:

    • 如果我这样使用,图标颜色不会改变。您能否更新您的代码。 onPressed: () { if (snapshot.hasData && snapshot.data) { deleteFromFavoriteList(widget.imageUrl); } else { saveFavoriteList(widget.imageUrl); }
    • 如果我想在 onPressed 中写一个方法,IconColor 不会改变 :(
    • 想想instagram中的like按钮。
    • @jancooth :如果要重新绘制(我的意思是根据新值更改该按钮的状态),您有两个选择,1)使用 setState 重新绘制该小部件. 2)修改那个future(首先将future分配给一个变量并在future builder中使用它,如果您重新创建并将其分配给该变量,它将再次执行代码)
    • @jancooth 请检查此线程:stackoverflow.com/questions/53170330/…
    【解决方案2】:

    是的,因为ifExistInFavoriteList(String url)Future&lt;bool&gt; 类型,您需要使用FutureBuilder 小部件来获取布尔值。

    Expanded(
          child: FutureBuilder(
              future: ifExistInFavoriteList(widget.imageUrl),
              builder: (context, asyncSnapshot){
                if(asyncSnapshot.hasData){
                  final _isLiked = asyncSnapshot.data;
                  return IconButton(
                    color:
                    _isLiked() ? Colors.deepPurple : Colors.green,
                    icon: Icon(Icons.category),
                    onPressed: () {
                      //TO-DO
                    },
                  );
                }
              return IconButton(
                color:Colors.grey,
                icon: Icon(Icons.category),
                onPressed: () {
                  //TO-DO
                },
              );
          },),
        ),
    

    【讨论】:

      【解决方案3】:

      一般性答案。

      假设这是你的函数,它返回Future&lt;bool&gt;

      Future<bool> myFunc() async => true;
      

      要从中获取bool 值,

      1. 使用async-await

        void main() async {
          var value = await myFunc(); // value = true
        }
        
      2. 使用then

        void main() {
          bool? value;
          myFunc().then((result) => value = result);
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-03
        • 1970-01-01
        • 2020-01-01
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        • 2021-05-22
        • 1970-01-01
        相关资源
        最近更新 更多