如何更改布尔字段值并在颤振中保存到数据库?

时间:2021-05-07 09:04:06

标签: flutter

我很难将布尔值更新为最喜欢的字段,我希望当我单击星号时,该值将更改为 false,并且数据将随着数据库中的更改而隐藏。我才学习了1周所以还是很差的,请珍惜任何建议!

我的代码:

class Favorite {
  String nodeId;
  String name;
  String type;
  bool favorite;
  int userId;
  String userName;
  int timeCreate;

  static DateTime _fromJson(int int) => DateTime.fromMillisecondsSinceEpoch(int);
  static int _toJson(DateTime time) => time.millisecondsSinceEpoch;
  Favorite({
    this.nodeId,
    this.name,
    this.type,
    this.favorite,
    this.userId,
    this.userName,
    this.timeCreate
  });

  factory Favorite.fromJson(Map<String, dynamic> json) {
    return Favorite(
      nodeId: json['nodeId'] as String,
      name: json['name'] as String,
      type: json['type'] as String,
      favorite: json['favorite'] as bool,
      userId: json['userId'] as int,
      userName: json['userName'] as String,
      timeCreate: json['timeCreate'] as int
    );
  }
}
Future<List<Favorite>> fetchFavorite() async{
  var requestHeaders = <String, String>{
    "Content-type": "application/json",
    "Accept": "application/json",
    "Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ2YW50aHUiLCJpYXQiOjE2MjAyMTA4NzcsImV4cCI6MTYyMjgwMjg3N30.9QrtUCQpy-Jd_4b7YwLY6GXlEab4M2_NDLRipNcFF_iEmIIzX1JKlotu7L_WJbXTJgtpFYY9kIW3GY6zhAx-JA"
  };
  final response = await http.get(Uri.parse(URL1), headers: requestHeaders);
  print(response);
  print(response.body);
  var decoded = utf8.decode(response.bodyBytes);
  var responseJson = json.decode(decoded);
  return (responseJson['data']['content'] as List)
      .map((p) => Favorite.fromJson(p))
      .toList();
}
Future<Favorite> setFavorite(String nodeId, bool favorite) async{
  var requestHeaders = <String, String>{
    "Content-type": "application/json",
    "Accept": "application/json",
    "Authorization": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ2YW50aHUiLCJpYXQiOjE2MjAyOTAxMDQsImV4cCI6MTYyMjg4MjEwNH0.G7vSN6jU9Y-87qEiU4UeyLZN6JkuRTFnmqkkYLWvIJZ8yCUXFSU8cAbIZ-RxD6-4Pab_9yqTC9Yo5fFpVcpcIQ"
  };
  final response = await http.put(
      Uri.parse(URL_SETFAVORITE),
    headers: requestHeaders,
  );
  if (response.statusCode == 200) {
    return Favorite.fromJson(json.decode(response.body));
  } else {
    throw Exception('Failed to update album.');
  }
}

我已经显示了我的收藏夹列表,现在想在将收藏夹值更改为 false 时隐藏星星

SizedBox(
                                  height: 30,
                                  child: Padding(

                                    padding: EdgeInsets.only(left: 114, top: 7),
                                    child: IconButton(
                                        padding: EdgeInsets.zero,
                                        icon: isStar
                                            ? Icon(
                                          Icons.star_border_outlined,
                                          size: 25,
                                        )
                                            : Icon(Icons.star,
                                            color: Colors.amber, size: 25),
                                        onPressed: () {
                                          setState(() {
                                            // Here we changing the icon.
                                            isStar = !isStar;
                                          });
                                        }),
                                  ))

0 个答案:

没有答案