Flutter flatter bool name=字符串是否可能?

Flutter flatter bool name=字符串是否可能?,flutter,boolean,Flutter,Boolean,嘿,我做了一个最喜欢的系统,用一个bool来表示是否最喜欢 但是如果bool的名称始终相同,那么它将应用于我的所有条目 但是每个条目都有自己的名称(widget.name),我想也许类似的东西可以工作 bool widget.name; 但这不起作用:( 如何解决每个条目都有自己的布尔值 顺便说一句,我用这个插件 完整代码 import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_pre

嘿,我做了一个最喜欢的系统,用一个bool来表示是否最喜欢

但是如果bool的名称始终相同,那么它将应用于我的所有条目

但是每个条目都有自己的名称(widget.name),我想也许类似的东西可以工作

bool widget.name;
但这不起作用:(

如何解决每个条目都有自己的布尔值

顺便说一句,我用这个插件

完整代码

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Details extends StatefulWidget {
  final String name;

  Details(
    this.name,
  );

  @override
  _DetailsState createState() => _DetailsState();
}

const String spKey = 'myBool';

class _DetailsState extends State<Details> {
  SharedPreferences sharedPreferences;
  bool isfavorit;

  @override
  void initState() {
    super.initState();

    SharedPreferences.getInstance().then((SharedPreferences sp) {
      sharedPreferences = sp;
      isfavorit = sharedPreferences.getBool(spKey);
      // will be null if never previously saved
      if (isfavorit == null) {
        isfavorit = false;
        persist(isfavorit); // set an initial value
      }
      setState(() {});
    });
  }

  void persist(bool value) {
    setState(() {
      isfavorit = value;
    });
    sharedPreferences?.setBool(spKey, value);
  }

// ignore: missing_return
  IconData favicon() {
    if (isfavorit == true) {
      return Icons.favorite;
    } else if (isfavorit == false) {
      return Icons.favorite_border;
    }
  }

// ignore: missing_return
  Color favicolor() {
    if (isfavorit == true) {
      return Colors.red;
    } else if (isfavorit == false) {
      return Colors.white;
    }
  }

  void changefav() {
    if (isfavorit == true) {
      return persist(false);
    } else if (isfavorit == false) {
      return persist(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            icon: Icon(
              favicon(),
              color: favicolor(),
            ),
            onPressed: () => changefav(),
          ),
        ],
        title: Text(widget.name),
      ),
      body: Container(
        child: Text(widget.name),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“package:shared_preferences/shared_preferences.dart”;
类详细信息扩展StatefulWidget{
最后的字符串名;
细节(
这个名字,
);
@凌驾
_DetailsState createState()=>\u DetailsState();
}
常量字符串spKey='myBool';
类_DetailsState扩展状态{
SharedReferences SharedReferences;
布尔喜欢它;
@凌驾
void initState(){
super.initState();
SharedReferences.getInstance()。然后((SharedReferences sp){
SharedReferences=sp;
isfavorit=SharedReferences.getBool(spKey);
//如果以前从未保存,则将为空
if(isfavorit==null){
isfavorit=false;
persist(isfavorit);//设置初始值
}
setState((){});
});
}
void persist(布尔值){
设置状态(){
isfavorit=值;
});
SharedReferences?.setBool(spKey,value);
}
//忽略:缺少返回
Iconda favicon(){
if(isfavorit==true){
返回图标。收藏;
}else if(isfavorit==false){
返回Icons.favorite_边框;
}
}
//忽略:缺少返回
彩色favicolor(){
if(isfavorit==true){
返回颜色。红色;
}else if(isfavorit==false){
返回颜色。白色;
}
}
void changefav(){
if(isfavorit==true){
返回persist(false);
}else if(isfavorit==false){
返回persist(true);
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
行动:[
图标按钮(
图标:图标(
favicon(),
颜色:favicolor(),
),
按下时:()=>changefav(),
),
],
标题:文本(widget.name),
),
主体:容器(
子项:文本(widget.name),
),
);
}
}

您总是将
isFavorite
保存到共享首选项中的同一个键,而不是使用基于
小部件的恒定键。name

例如:

SharedReferences.getBool('details_favorite_${widget.name}');

请分享你拥有的相关代码。你能分享整个widgetHave更新帖子吗?我到底要在哪里使用它或替换它?无论你在哪里使用
spKey
使用
'details\u favorite{widget.name}'
而不是工作:)现在我如何在列表vie listbuilder中加载所有保存的bool名称?在另一页?检查共享首选项的方法谢谢,但我还是一个真正的初学者。你能给我一个代码示例,说明我如何实现它吗?
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Details extends StatefulWidget {
  final String name;

  Details(
    this.name,
  );

  @override
  _DetailsState createState() => _DetailsState();
}

const String spKey = 'myBool';

class _DetailsState extends State<Details> {
  SharedPreferences sharedPreferences;
  bool isfavorit;

  @override
  void initState() {
    super.initState();

    SharedPreferences.getInstance().then((SharedPreferences sp) {
      sharedPreferences = sp;
      isfavorit = sharedPreferences.getBool(spKey);
      // will be null if never previously saved
      if (isfavorit == null) {
        isfavorit = false;
        persist(isfavorit); // set an initial value
      }
      setState(() {});
    });
  }

  void persist(bool value) {
    setState(() {
      isfavorit = value;
    });
    sharedPreferences?.setBool(spKey, value);
  }

// ignore: missing_return
  IconData favicon() {
    if (isfavorit == true) {
      return Icons.favorite;
    } else if (isfavorit == false) {
      return Icons.favorite_border;
    }
  }

// ignore: missing_return
  Color favicolor() {
    if (isfavorit == true) {
      return Colors.red;
    } else if (isfavorit == false) {
      return Colors.white;
    }
  }

  void changefav() {
    if (isfavorit == true) {
      return persist(false);
    } else if (isfavorit == false) {
      return persist(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            icon: Icon(
              favicon(),
              color: favicolor(),
            ),
            onPressed: () => changefav(),
          ),
        ],
        title: Text(widget.name),
      ),
      body: Container(
        child: Text(widget.name),
      ),
    );
  }
}