如何在bloc [flutter]上列出项目复制

发布于 2025-01-17 12:14:25 字数 638 浏览 1 评论 0 原文

我想改变List> Bloc 上的列表。

下面是我的示例块代码。

请教我如何正确使用copyWith。

[示例代码]

class StateA extends Equatable {
  List<Map<String, dynamic>> listMap;

  StateA({ required this.listMap});

  StateA copyWith(
   {List<Map<String, dynamic>>? listMap,}) {
  return StateA(
    listMap: listMap ?? this.listMap);
}
}

class CubitA extends Cubit<StateA> {
 CubitA() : super(StateA(listMap: []));

 void testCopy() {
  emit(state.copyWith(listMap: [{"test_key": "test_value"}]));
  print(state.listMap); // => []
}

(我英语不好,抱歉)

I want to change List<Map<String, dynamic>> list on Bloc.

Below is my sample bloc code.

Please teach me how to use copyWith correctly.

[Sample Code]

class StateA extends Equatable {
  List<Map<String, dynamic>> listMap;

  StateA({ required this.listMap});

  StateA copyWith(
   {List<Map<String, dynamic>>? listMap,}) {
  return StateA(
    listMap: listMap ?? this.listMap);
}
}

class CubitA extends Cubit<StateA> {
 CubitA() : super(StateA(listMap: []));

 void testCopy() {
  emit(state.copyWith(listMap: [{"test_key": "test_value"}]));
  print(state.listMap); // => []
}

(I'm not good at English, sorry)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸里的那抹悲凉 2025-01-24 12:14:25

我解决它添加对象?

代码如下。

class StateA extends Equatable {
  List<Map<String, dynamic>> listMap;

  StateA({ required this.listMap});

  StateA copyWith(
   {List<Map<String, dynamic>>? listMap,}) {
  return StateA(
    listMap: listMap ?? this.listMap);
  };

  // Add below code
  List<Object?> get props => [listMap];
}

class CubitA extends Cubit<StateA> {
 CubitA() : super(StateA(listMap: []));

 void testCopy() {
  emit(state.copyWith(listMap: [{"test_key": "test_value"}]));
  print(state.listMap); // => [{"test_key": "test_value"}]
}

I solve it to add object?.

The code is below.

class StateA extends Equatable {
  List<Map<String, dynamic>> listMap;

  StateA({ required this.listMap});

  StateA copyWith(
   {List<Map<String, dynamic>>? listMap,}) {
  return StateA(
    listMap: listMap ?? this.listMap);
  };

  // Add below code
  List<Object?> get props => [listMap];
}

class CubitA extends Cubit<StateA> {
 CubitA() : super(StateA(listMap: []));

 void testCopy() {
  emit(state.copyWith(listMap: [{"test_key": "test_value"}]));
  print(state.listMap); // => [{"test_key": "test_value"}]
}
辞旧 2025-01-24 12:14:25

因此,应使用此上下文中的 copy with 函数将地图添加到地图列表中。

StateA copyWith(
   {List<Map<String, dynamic>> newListMap}) {
  return StateA(
    listMap: [listMap, newListMap]);
}

此函数将允许您将项目添加到当前状态的 listMap 并发出新状态。

建议:

  1. 不要将 copyWith 函数的参数作为可选参数(去掉问号)。

  2. 您必须重写 props 函数才能使 equatable 正常工作。请参阅文档或一些视频来了解这一点。

  3. 如果您对 flutter 很陌生,请忽略这一点..但我建议您浏览冻结的课程视频以创建块状态。我将在下面链接一个视频。它生成 copyWith 和 equals 操作的所有样板代码。
    https://youtu.be/ApvMmTrBaFI

So the copy with function in this context should be used to add maps to the list of maps.

StateA copyWith(
   {List<Map<String, dynamic>> newListMap}) {
  return StateA(
    listMap: [listMap, newListMap]);
}

This function will allow you to add items to the current state's listMap and emit a new state.

Suggestions:

  1. Don't make the the copyWith function's parameter as an optional parameter (remove the question mark).

  2. You'll have to override the props function for equatable to work. Refer to the docs or some video to get an understanding of that.

  3. If you're very new to flutter ignore this.. but I'd recommend going through the freezed class videos to make bloc state. I'll link a video below. It generates all the boilerplate code for copyWith and equality operations.
    https://youtu.be/ApvMmTrBaFI

记忆消瘦 2025-01-24 12:14:25

这个工作

Bloc 状态:

enum ProductStatus { initial, loading, success, failure }

class ProductState extends Equatable {
  const ProductState(
      {this.status, this.error, this.productResponse, this.categoryResponse});

  final ProductStatus? status;
  final String? error;
  final ProductResponse? productResponse;
  final CategoryResponse? categoryResponse;

  @override
  List<Object?> get props => [status, error, productResponse, categoryResponse];

  ProductState copyWith({
    ProductStatus? status,
    String? error,
    ProductResponse? productResponse,
    CategoryResponse? categoryResponse,
  }) {
    return ProductState(
      status: status ?? this.status,
      error: error ?? this.error,
      productResponse: productResponse ?? this.productResponse,
      categoryResponse: categoryResponse ?? this.categoryResponse,
    );
  }
}

像这样发出:

 CategoryResponse categoryResponse =
          CategoryResponse.fromJson(jsonDecode(result.response.toString()));
      emit(state.copyWith(
          categoryResponse: categoryResponse));

this working

Bloc state:

enum ProductStatus { initial, loading, success, failure }

class ProductState extends Equatable {
  const ProductState(
      {this.status, this.error, this.productResponse, this.categoryResponse});

  final ProductStatus? status;
  final String? error;
  final ProductResponse? productResponse;
  final CategoryResponse? categoryResponse;

  @override
  List<Object?> get props => [status, error, productResponse, categoryResponse];

  ProductState copyWith({
    ProductStatus? status,
    String? error,
    ProductResponse? productResponse,
    CategoryResponse? categoryResponse,
  }) {
    return ProductState(
      status: status ?? this.status,
      error: error ?? this.error,
      productResponse: productResponse ?? this.productResponse,
      categoryResponse: categoryResponse ?? this.categoryResponse,
    );
  }
}

Emit like this:

 CategoryResponse categoryResponse =
          CategoryResponse.fromJson(jsonDecode(result.response.toString()));
      emit(state.copyWith(
          categoryResponse: categoryResponse));
提赋 2025-01-24 12:14:25

您必须为您的 StateA 创建一个对象,然后通过该对象访问副本。

you have to create a object for your StateA and then access the copywith through that object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文